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

前端 未结 14 2548
别跟我提以往
别跟我提以往 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);
    
    0 讨论(0)
  • 2020-11-29 18:27

    Please replace ===== app.set('view engin', 'html'); with app.set('view engine', 'ejs');

    0 讨论(0)
  • 2020-11-29 18:27

    if you've got this error by using the express generator, I've solved it by using

    express --view=ejs myapp
    

    instead of

    express --view=pug myapp
    
    0 讨论(0)
  • 2020-11-29 18:29

    The res.render stuff will throw an error if you're not using a view engine.

    If you just want to serve json replace the res.render('error', { error: err }); lines in your code with:

    res.json({ error: err })
    

    PS: People usually also have message in the returned object:

    res.status(err.status || 500);
    res.json({
      message: err.message,
      error: err
    });
    
    0 讨论(0)
  • 2020-11-29 18:33

    Just set view engine in your code.

    var app = express(); 
    app.set('view engine', 'ejs');
    
    0 讨论(0)
  • 2020-11-29 18:35

    If all that's needed is to send html code inline in the code, we can use below

    var app = express();
    app.get('/test.html', function (req, res) {
       res.header('Content-Type', 'text/html').send("<html>my html code</html>");
    });
    
    0 讨论(0)
提交回复
热议问题