使用Node.js + Express而不使用模板引擎
我已经尝试查看StackOverflow解决的有关收到此错误的人的所有问题:
Error: No default engine was specified and no extension was provided.
我觉得我已经搜索过每一篇文章了,没有一个答案纠正了我的代码。必须有其他事情发生。
我的问题是我在决定使用Node.js + Express作为我的后端之前已经创建了许多html文件...所以不要担心将它们全部转换为像pug或EJS这样的模板引擎,我只想从我的目录中的静态公用文件夹中提供它们。
根据我的研究,你不需要模板引擎来使用Node.js + Express。但您确实需要设置静态公用文件夹来为您的文件提供服务。
我在我的app.js文件中包含了app.use(express.static(path.join(__dirname, 'public')));
。我添加了一个'public'文件夹并将所有静态文件放在其中,并在我的routes文件(我的index.js文件)中,我编写了以下路由:
router.get('/', (req, res) => {
res.sendFile('./public/index.html');
});
我尝试将其更改为以下内容:
res.sendFile('index');
res.sendFile(path.join(__dirname, 'index.html')
res.sendFile('../public/index.html');
我也尝试将我的静态中间件语法改为app.use(express.static('public'));
,但似乎也没有改变任何东西。
所有这些都渲染了我在上面提供的No default engine was specified and no extension was provided.
错误。我讨厌在StackOverflow上有这么多相同的问题时我需要问这个问题,但我目前完全不知道该怎么做。没有进一步的说明,这是我的代码:
这是我的文件目录结构。
这是我处理所有路由的index.js文件:
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
router.post('/', (req, res) => {
});
module.exports = router;
这是我的app.js文件:
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const routes = require('./routes/index');
const errorHandlers = require('./handlers/errorHandlers');
const app = express();
app.use('/', routes);
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// If that above routes didnt work, we 404 them and forward to error handler
app.use(errorHandlers.notFound);
// One of our error handlers will see if these errors are just validation errors
app.use(errorHandlers.flashValidationErrors);
// Otherwise this was a really bad error we didn't expect! Shoot eh
if (app.get('env') === 'development') {
/* Development Error Handler - Prints stack trace */
app.use(errorHandlers.developmentErrors);
}
// production error handler
app.use(errorHandlers.productionErrors);
module.exports = app;
这是我的package.json:
{
"name": "********",
"version": "1.0.0",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-concat": "^1.0.1",
"grunt-contrib-imagemin": "^1.0.1",
"grunt-contrib-sass": "^1.0.0",
"grunt-contrib-uglify": "^2.3.0",
"grunt-contrib-watch": "^1.0.0",
"webpack-dev-server": "^2.9.7"
},
"description": "",
"main": "app.js",
"scripts": {
"start": "nodemon ./start.js"
},
"author": "****** ********",
"license": "ISC",
"dependencies": {
"cookie-parser": "^1.4.3",
"dotenv": "^4.0.0",
"express": "^4.16.2",
"mongod": "^2.0.0",
"mongoose": "^4.13.7",
"nodemon": "^1.14.1",
"normalize.css": "^6.0.0"
}
}
这是我运行npm start
时运行的start.js文件:
const mongoose = require('mongoose');
// import environmental variables from our variables.env file
require('dotenv').config({ path: 'variables.env' });
mongoose.connect(process.env.DATABASE, { useMongoClient: true });
mongoose.Promise = global.Promise;
mongoose.connection.on('error', (err) => {
console.error(`${err.message}`);
});
const app = require('./app');
app.set('port', process.env.PORT || 7777);
const server = app.listen(app.get('port'), () => {
console.log(`Express running → PORT ${server.address().port}`);
});
回答如下:
传递这个作为答案
试试res.sendFile(path.join(__dirname + '/../public/index.html'))