react-router-tutorial icon indicating copy to clipboard operation
react-router-tutorial copied to clipboard

Error : Cannot GET /

Open Aadriya opened this issue 7 years ago • 3 comments

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

Aadriya avatar Apr 17 '17 09:04 Aadriya

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"> <html> <meta charset=utf-8/> <title>Antonio Scala Web Developer</title> <div id=app>${appHtml}</div> <script src="/bundle.js"></script> }`

Besides I have this error: " const appHtml = ReactDOMServer.renderToString(<RouterContext {...props} />); ^ SyntaxError: Unexpected token <"

Any help? thanks

marshall86 avatar Apr 18 '17 16:04 marshall86

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

Aadriya avatar Apr 22 '17 10:04 Aadriya

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?

yuyic avatar Jun 14 '17 03:06 yuyic