I am working with node.js
and express
. Now what I am looking for is to make "not found 404" page for my website;
this is my "app.js":
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, 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(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
app.get('/notfound', routes.index);
http.createServer(app).listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
I was trying with app.get('/notfound', routes.index);
but it gives me an error ofCannot GET /notfound
Also try: app.get('/*', routes.index);
but it gives me an error of Cannot GET /*
.
The idea is to redirect when a response is returned
404
, something like this:You can see the full example here .
You can render your own .html file in one line like this ;)