我正在开发一个博客来学习 Node 和 javascript,但我遇到了问题。我无法获取我的页面,我也不知道为什么。有人可以引导我或给我解释这个问题,谢谢。 我把我的 index.js 和我的 package.json 放在下面。如您所见,我已经安装了 nodemon 和 bootstrap,并且我正在 cmd 中执行所有这些操作。
index.js
const path = require('path');
const expressEdge = require('express-edge');
const express = require('express');
const app = new express();
app.use(express.static('public'));
app.use(expressEdge);
app.set('views', __dirname + '/views');
app.get('/', (req, res) => {
res.sendFile(path.resolve(__dirname, './pages/index.html'));
});
app.get('/about', (req, res) => {
res.sendFile(path.resolve(__dirname, './pages/about.html'));
});
app.get('/contact', (req, res) => {
res.sendFile(path.resolve(__dirname, 'pages/contact.html'));
});
app.get('/post', (req, res) => {
res.sendFile(path.resolve(__dirname, 'pages/post.html'));
});
app.listen(3000, () => {
console.log('App listening on port 3000')
});
package.json
{
"name": "projet3web",
"version": "1.0.0",
"description": "Create a blog",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"},
"keywords": [
"blog"],
"author": "Kym lusinchi Vincent Blanc El Hachemi Sabi",
"license": "ISC",
"dependencies": {
"bootstrap": "^4.3.1",
"bootstrap-datepicker": "^1.8.0",
"express": "^4.16.4",
"express-edge": "^1.0.0",
"nodemon": "^1.18.11",
"popper.js": "^1.15.0",
"startbootstrap-clean-blog": "file:startbootstrap-clean-blog",
"tooltip.js": "^1.3.1"
}
}
最佳答案
检查你在res.send中的路由是否正确。我认为您不需要 .html
我不知道你的文件树是什么样子的,但如果它是标准的,你就不需要路径开头的点
无论如何我认为你应该使用 res.render('/');
关于javascript - 是否有任何原因导致我无法获取我的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55610154/