当我使用npm start启动服务器,然后访问位于localhost:3000的页面时,出现以下错误:
Error: Cannot find module 'ejs'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:587:15)
at Function.Module._load (internal/modules/cjs/loader.js:513:25)
at Module.require (internal/modules/cjs/loader.js:643:17)
at require (internal/modules/cjs/helpers.js:22:18)
at new View (C:\Users\Junior\Desktop\Node projects\node_modules\express\lib\view.js:81:14)
at Function.render (C:\Users\Junior\Desktop\Node projects\node_modules\express\lib\application.js:570:12)
at ServerResponse.render (C:\Users\Junior\Desktop\Node projects\node_modules\express\lib\response.js:1008:7)
at router.get (C:\Users\Junior\Desktop\Node projects\Practica-Express\routes\shop.js:12:9)
at Layer.handle [as handle_request] (C:\Users\Junior\Desktop\Node projects\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Junior\Desktop\Node projects\node_modules\express\lib\router\route.js:137:13)
我使用-g和--dev安装了ejs,但这些都不起作用。我还添加了这行代码app.set('views','views');在app.set('view engine','ejs');
下这是主要的js文件:
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.set('view engine', 'ejs');
const adminData = require('./routes/admin');
const shopRoutes = require('./routes/shop');
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/admin', adminData.routes);
app.use(shopRoutes);
app.use((req, res, next) => {
res.status(404).render('404', {pageTitle: 'Page Not Found!'});
});
app.listen(3000);
这是我的package.json文件:
{
"name": "practica-express",
"version": "1.0.0",
"description": "",
"main": "express.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon express.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^1.19.0"
},
"dependencies": {
"body-parser": "^1.19.0",
"ejs": "^2.6.1",
"express-handlebars": "^3.0.2",
"pug": "^2.0.3"
}
}
查看模板如何不获取ejs引擎: