I'm getting a 'No default engine was specified and no extension was provided' on running my app, and I can't understand why.
在运行我的应用程序时,我收到了一个“没有指定默认引擎,也没有提供扩展”的提示,我不明白为什么。
The setup I've got looks rougly like this:
我得到的设置很粗糙,如下所示:
index.js
Index.js
const express = require('express');
const app = express();
app.set('view engine', 'ejs'); // set view engine
const routes = require('./routes'); // import routes file
app.use('/', routes); // use routes
app.listen(3000, () => console.log('Server running on port 3000'));
routes.js
Routes.js
const express = require('express');
const router = express.Router();
const userRoutes = require('./userRoutes');
const locationRoutes = require('./locationRoutes');
router.use('/users', userRoutes);
router.use('/locations', locationRoutes);
module.exports = router;
then there's files like this for users, locations etc:
然后还有这样的文件,供用户、位置等使用:
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
res.render('users');
});
module.exports = router;
When I run it, it fails with 'no default engine'. I think I've set this up right, and ejs is definitely installed and working.
当我运行它时,它失败了,没有默认引擎。我想我已经设置好了,ejs肯定已经安装好了,并且可以正常工作。
更多回答
优秀答案推荐
You need to set the views
directory as well. Update your code like so:
您还需要设置视图目录。如下所示更新代码:
const express = require('express');
const app = express();
const path = require('path'); //< Add this
app.set('views', path.join(__dirname, 'views')); //< Add this
app.set('view engine', 'ejs');
Make sure you have a users.ejs
file in the views
directory and you should be good.
确保在view目录中有一个users.ejs文件,这样就可以了。
更多回答
我是一名优秀的程序员,十分优秀!