简体   繁体   中英

In express router method is not working if i serve static files

In my node application first i want to serve my static files and then using router method depending upon the incoming request i will query the DB and get the body of the html page then i will send this body content to the client side and finally using Backbone.js i will render this body in my html page.Now what is my problem means express router method is not working if i serve static files.But if i remove the below line means my router is working.

app.use(express.static(__dirname + '/public'));

app.js

var express=require('express');
var app=express();

app.use(express.static(__dirname + '/public'));

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

   console.log('router called successfully...');
   res.send('body of the page');
   res.end();

});

app.listen(8011);
app.use(express.static(__dirname + '/public'));

Your unnecessary anonymous function is screwing up how the middleware is supposed to work. The above line is in 95% or more of all express sample apps. Not sure why you decided to deviate from that, but your version is a useless middleware that creates a static middleware and immediately discards it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM