简体   繁体   中英

Custom 404 page with Express.js

I'm trying to redirect to an html page when a 404 error is thrown. I have a folder titled: 404 in my public folder. Here is the code in the index.js:

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

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

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

res.sendFile(__dirname+'/public/html/contacts.html')


})

app.use(function(req, res, next){

// res.type('txt').send('Not found');
// res.status(404).end('error');

  if (req.accepts('html')) {
    res.render('404', { url: req.url });
    return;
  }

});



var server = app.listen(8000, function(){

console.log("Server is happening %s", server.address().port);

});

But I get the following error:

Error: No default engine was specified and no extension was provided. at new View (/home/pi/server/node_modules/express/lib/view.js:62:11) at EventEmitter.render (/home/pi/server/node_modules/express/lib/application.js:569:12) at ServerResponse.render (/home/pi/server/node_modules/express/lib/response.js:961:7) at /home/pi/server/index.js:19:9 at Layer.handle [as handle_request] (/home/pi/server/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/home/pi/server/node_modules/express/lib/router/index.js:312:13) at /home/pi/server/node_modules/express/lib/router/index.js:280:7 at Function.process_params (/home/pi/server/node_modules/express/lib/router/index.js:330:12) at next (/home/pi/server/node_modules/express/lib/router/index.js:271:10) at SendStream.error (/home/pi/server/node_modules/express/node_modules/serve-static/index.js:120:7) Error: No default engine was specified and no extension was provided. at new View (/home/pi/server/node_modules/express/lib/view.js:62:11) at EventEmitter.render (/home/pi/server/node_modules/express/lib/application.js:569:12) at ServerResponse.render (/home/pi/server/node_modules/express/lib/response.js:961:7) at /home/pi/server/index.js:19:9 at Layer.handle [as handle_request] (/home/pi/server/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/home/pi/server/node_modules/express/lib/router/index.js:312:13) at /home/pi/server/node_modules/express/lib/router/index.js:280:7 at Function.process_params (/home/pi/server/node_modules/express/lib/router/index.js:330:12) at next (/home/pi/server/node_modules/express/lib/router/index.js:271:10) at SendStream.error (/home/pi/server/node_modules/express/node_modules/serve-static/index.js:120:7)

In case you can adapt your 404 page to be a static page, you can simply do:

res.sendFile('/path/to/404.html');

Conversely, if you want to have some view model object to be rendered (eg, {url: req.url} ), you have to decide for a template engine (eg, Jade, EJS, Handlebars). As an example, assuming you want to use jade, you first install it as a dependency:

npm install jade

Then, you should add the following lines at the beginning of your script:

app.set('view', path.join(__dirname, 'private/view/path'));
app.set('view engine', 'jade');

Once the template engine is in place, you can do:

res.render('viewName', viewModelObject)

and the viewName will be resolved as ./private/view/path/viewName.jade . Hence, assuming the existence of ./private/view/path/404.jade , you can do:

res.render('404', { url: req.url });

Please remember to keep your templates (ie, your .jade files) somewhere inaccessible to the user.

app.use(function(req, res, next) {
      var err = new Error('Not Found');
      err.status = 404;
      next(err);
    });

for response.render method, you have to use template engine like ejs.

and you have to setup view options.

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

But if you have put 404 file in public folder, then you can simply use sendfile or redirect to static path.

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