Express: static files not found when using ejs

I want all my requests to be redirected to a index.ejs view, located in the Views folder. In my routes.js file, I've added this:

  module.exports = function (app) {

  app.get('/', function(req, res) {
     res.render('index', {title: "TODO"})

The index view contains all of page's markups as it will be a SPA, so I will dynamically swap views on the client-side.

The problem is that I references scripts located in the public folder and they are not found, I get a 404 :

script src="public/app/app.js"></script>

I think it's because Express does not know how to server the file. I've specified that everything in the public folder should be static files:

   app.use(express.static(config.root + '/public'))  

Now if I don't use ejs template and if I use a index.html file in the public folder instead of a view, it works fine. I just have to do that instead:

 app.get('*', function(req, res) {

But I want to take advantage of ejs template for adding bundles to page instead of adding scripts one by one.

So what is wrong with my code ? How can I solve that 404 on the javascript files ?


Got it. The path to the script file was incorrect. It should be app/app.js instead of public/app/app.js

I think this is how the static middleware works.

Need Your Help

Aligning an ArrayList of Strings into a (custom)Box won't get in proper position

java fonts position graphics2d

I've got a little problem with aligning a group of Strings to the bottom of a box in Java.

OpenGL: Why far and near are negative in orthogonal projection?

opengl graphics 3d orthographic

Why are the near and far coordinates are interpreted as negative in orthographic projection? Is it just customary or there is really any benefit of this?