Jonas Bergner Jonas Bergner - 10 months ago 66
Javascript Question

express' response.sendFile() sends Uncaught SyntaxError: Unexpected token <

I'm using react-router with browserHistory. This function:

app.get('*', function (request, response){
response.sendFile(path.join(__dirname + "/public/index.html"))

is supposed to send the Index.html since the routing happens client-side with react-router.
But somehow my index.html is misunderstood by the server and I get this error:

Uncaught SyntaxError: Unexpected token <

I don't know what the problem here is. Is the file path wrong? My tree looks like this:

/server.js <- my express file

without the above described function my page usually responds with:

Cannot GET /whatever

on every page refresh that doesn't happen on localhost:3000 (e.g. localhost:3000/whatever)

Because I'm aware that i suck at describing things, here is a link to the repository.

Help is very appreciated. :)

Answer Source

First of all you did not build your bundle.js file with your webpack. Before you can serve it you need to build it first.

Change you package.json script to

"scripts": {
    "build": "webpack --progress --color --config  webpack.config.js --watch",
    "start": "node server.js"

and run the command npm run build before running your server.

If you webpack bundle.js doesn't get created inside /public/build then create a directory build inside public and run the above command again.

This should solve your problem.