[Chartjs]-Uncaught SyntaxError in express application

2👍

you have your Express app configured like this:

const server = express()
  .use((req, res) => res.sendFile(INDEX))
  .listen(PORT, () => console.log(`Listening on ${ PORT }`));

that call to .use() with no path specified like the docs recommend? it means that for every single request received by your app, the file specified by INDEX will be emitted. So when the browser parses index.html and sees a <script> tag pointing to main.js, it asks your app for /main.js and gets INDEX. When your browser expects Javascript but gets HTML, it throws the SyntaxError you see in your console.

try using explicit routes and middleware functions (and HTTP request types) in your app:

app.get('/index.html', function(req, res) {
  res.sendFile(INDEX);
});
app.get('/main.js', function(req, res) {
  res.sendFile('/path/to/main.js');
});

(that’s a naive example but it should get you started.)

Leave a comment