0

I just inherited a node.js project, where I'm suspicious that some of the original boilerplate necessary for a node project hasn't been finished. This is also my first time using node.js.

Anyway, when I run node app.js, the correct port boots up and everything, but the app can't load a number of the script files (the error "Uncaught SyntaxError: Unexpected token <" is given on each js file unable to be loaded). I've tried to troubleshoot this error, there are a lot of resources online for it, but I haven't been able to nail it down. I tried adding type="text/javascript" to each of the script tabs but it didn't change anything.

When I click to troubleshoot the incorrect loading paths of the scripts, they all bring me to the index.html page. Thus, for some reason, it seems like this section of the code from the app.js file isn't working:

// serve all asset files from necessary directories
app.use('/js', express.static(__dirname + '/public/js'));
app.use('/css', express.static(__dirname + '/public/css'));
app.use('/partials', express.static(__dirname + '/public/partials'));
app.use('/lib', express.static(__dirname + '/public/lib'));

// JSON API
app.get('/api/name', api.name);

// redirect all others to the index (HTML5 history)
app.all('/*', function(req, res, next) {
    res.sendfile('index.html', { root: __dirname + '/public' });
});

However, it looks like it should be working properly (following the paths of the files). Any ideas?

1 Answer 1

1

Almost certainly the app.all route is sending back HTML for a request that should be sending javascript. You need to look at the specific URLs that the browser is fetching as script tags but the server is sending back HTML. The corresponding .js files probably are just not there under public/js which causes the router to proceed to the catch-all route.

Side note you can compress your express.static routes to just this because of how your URLs line up with the filesystem structure.

// serve all asset files from necessary directories
app.use(express.static(__dirname + '/public'));
Sign up to request clarification or add additional context in comments.

1 Comment

Oh... so I can compress the four app.use lines to the one you gave? That makes sense. I'll some more troubleshooting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.