Error: No default engine was specified and no extension was provided

前端 未结 14 2600
别跟我提以往
别跟我提以往 2020-11-29 18:13

I am working through setting up a http server using node.js and engine. However, I keep running into issues that I have little information on how to resolve I would apprecia

14条回答
  •  盖世英雄少女心
    2020-11-29 18:21

    You can use express-error-handler to use static html pages for error handling and to avoid defining a view handler.

    The error was probably caused by a 404, maybe a missing favicon (apparent if you had included the previous console message). The 'view handler' of 'html' doesn't seem to be valid in 4.x express.

    Regardless of the cause, you can avoid defining a (valid) view handler as long as you modify additional elements of your configuration.

    Your options are to fix this problem are:

    • Define a valid view handler as in other answers
    • Use send() instead of render to return the content directly

    http://expressjs.com/en/api.html#res.render

    Using render without a filepath automatically invokes a view handler as with the following two lines from your configuration:

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

    and:

    res.render('500);
    

    Make sure you install express-error-handler with:

    npm install --save express-error-handler
    

    Then import it in your app.js

    var ErrorHandler = require('express-error-handler');
    

    Then change your error handling to use:

    // define below all other routes
    var errorHandler = ErrorHandler({
      static: {
        '404': 'error.html' // put this file in your Public folder
        '500': 'error.html' // ditto
    });
    
    // any unresolved requests will 404
    app.use(function(req,res,next) {
      var err = new Error('Not Found');
      err.status(404);
      next(err);
    }
    
    app.use(errorHandler);
    

提交回复
热议问题