質問

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) {
    res.sendfile('./public/index.html');
 });

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top