Serve static files and app.get conflict using Express.js

I have this piece of code:

var express = require('express') , http = require('http') var app = express(); var server = app.listen(1344); var io = require('socket.io').listen(server); app.use(express.static(__dirname + '/public')); app.use(express.bodyParser()); app.use(express.cookieParser()); app.use(express.session({secret: 'secret'})); app.get('/', function(req, res){ if(req.session){ console.log(req.session); } console.log('ok'); }); 

The code inside the app.get() callback is not called. If I comment on the line app.use(express.static(__dirname + '/public')) , then callaback works. I tried to reorder, but it looks like a lottery! I would rather know what is going on here.

I am sure that this is due to a lack of knowledge on my part about what middleware is called. Can someone help me understand this problem?

Basically, I just want to execute some logic before the files are served and index.html is loaded in the browser. By the way, placing app.get() before the line app.use(express.static()) doesn't do the trick!

+7
source share
3 answers

Your static middleware should be the first.

 app.use(express.static(__dirname + '/public')); app.use(express.bodyParser()); app.use(express.cookieParser()); app.use(express.session({secret: 'secret'})); 

And you should also add usage for app.router.

 app.use(express.static(__dirname + '/public')); app.use(express.bodyParser()); app.use(express.cookieParser()); app.use(express.session({secret: 'secret'})); app.use(app.router); 

Secondary software is processed for each request. Therefore, if you have index.html in your static files, then requests for yourdomain.com/ will never get into app.router , because they will be served by a static file handler. Delete index.html and then this request will be passed to your app.router .

+5
source

Rename the index.html file to another. It's simple

 app.use(express.static(__dirname + '/public')); app.get('/', function(req, res){ if(req.session){ console.log(req.session); } console.log('ok'); res.sendfile(new_index_file); }); 
+3
source

I believe you have 3 options:

1) Set your route app.get('/') (possibly using app.router ) before the static middleware so that they take precedence. The middleware, which is mounted first, first processes the corresponding request.

2) Use the path prefix for your static paths, such as app.use('/static', express.static('public')); so the statics are served with example.com/static/...

3) Do you want to act smart and shoot in the foot? :) Sometimes people use Accept headers and content negotiation to serve 2 different types of content from the same URL in different circumstances. You can configure static middleware checking for a specific type of content in the Accept header and the process request only if the Accept request header wants to be of the correct type. Otherwise, it will process the stream downstream / . You can configure content negotiation for static intermediate content in req.accepts .

0
source

All Articles