reactjs / react-router-tutorial

5.52k stars 1.75k forks source link

Error : Cannot GET / #315

Open Aadriya opened 7 years ago

Aadriya commented 7 years ago

When i run http://localhost:8080/ get Cannot GET /

marshall86 commented 7 years ago

some issue, below my server.js file (I used require instead of import because it doesn't works for me)

`var React = require('react'); var express = require('express'); var path = require('path'); var compression = require('compression'); var PORT = process.env.PORT || 1000;

// we'll use this to render our app to an html string var ReactDOMServer = require('react-dom/server');

// and these to match the url to routes and then render var match = require('react-router').match; var RouterContext = require('react-router').RouterContext; var routes = require('./app/modules/Routes');

var app = express();

app.use(compression());

// serve our static stuff like index.css
app.use(express.static(path.join(__dirname, 'app')));

// send all requests to index.html so browserHistory works
app.get('*', (req, res) => {
    match({ routes: routes, location: req.url }, (err, redirect, props) => {
        // in here we can make some decisions all at once
        if (err) {
            // there was an error somewhere during route matching
            res.status(500).send(err.message)
        } else if (redirect) {
            // we haven't talked about `onEnter` hooks on routes, but before a
            // route is entered, it can redirect. Here we handle on the server.
            res.redirect(redirect.pathname + redirect.search)
        } else if (props) {
            // if we got props then we matched a route and can render
            const appHtml = ReactDOMServer.renderToString(<RouterContext {...props} />);

            res.send(renderPage(appHtml))
        } else {
            // no errors, no redirect, we just didn't match anything
            res.status(404).send('Not Found')
        }
    })
});

app.listen(PORT, function() {
    console.log('Production Express server running at localhost:' + PORT);
});

function renderPage(appHtml) { return ` <!doctype html public="storage">

Antonio Scala Web Developer
${appHtml}
` }` Besides I have this error: " const appHtml = ReactDOMServer.renderToString(); ^ SyntaxError: Unexpected token <" Any help? thanks
Aadriya commented 7 years ago

I think you use isomorphic method for serverside rendering (Isomorphic didnt support newest Version of react). Refer https://github.com/jackfranklin/universal-react-example ..this is a best example for Universal serverside rendering

yuyic commented 7 years ago

I have this problem too. @Aadriya could you please explain more detail? what is isomorphic method? is this because the node version is too old?