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

前端 未结 14 2552
别跟我提以往
别跟我提以往 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:41

    The above answers are correct, but I found that a simple typo can also generate this error. For example, I had var router = express() instead of var router = express.Router() and got this error. So it should be the following:

    // App.js 
    var express = require('express');
    var app = express();
    var bodyParser = require('body-parser');
    app.use(bodyParser.urlencoded({ extended:false}));
    // assuming you put views folder in the same directory as app.js
    app.set('views', __dirname + '/views')
    app.engine('ejs', ejs.renderFile);
    app.set('view engine', 'ejs');
    // router - wherever you put it, could be in app.js
    var router = express.Router();
    router.get('/', function (req,res) {
      res.render('/index.ejs');
    })
    

提交回复
热议问题