Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Send 404 header from not existent static content when using * route in Express

Send 404 header from not existent static content when using * route in Express

Problem

I'v got Express app like this.

var express = require('express');
var http = require('http');
var path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(app.router);

app.get('*', function(res,req) {
    req.render('index', {title: "Title"});
});

http.createServer(app).listen(app.get('port'), function () {
    console.log('Express server listening on port ' + app.get('port'));
});

Because of using app.get('*') request to not existent static files responses with rendered index.ejs file instead of error 404.

Is there any way to fix it without additional conditions in routes?

Problem courtesy of: Ashot

Solution

Resolved problem by nginx proxying

Solution courtesy of: Ashot

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Send 404 header from not existent static content when using * route in Express

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×