是否有任何原因导致我无法获取我的页面

时间:2019-04-10 10:21:41

标签: javascript node.js express

我正在开发一个博客来学习节点和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"
 }
}

2 个答案:

答案 0 :(得分:1)

检查重新发送中的路由是否正确。我认为您不需要.html

我不知道您的文件树是什么样子,但是如果它是标准结构,则您将不需要在路径开始处加点

无论如何,我认为您应该使用res.render('/');

答案 1 :(得分:0)

执行app.use(express.static('public'))时,会将public文件夹下的所有文件作为static文件提供。

(根据您的代码)假设您的文件夹结构为

.
+-- index.js
+-- public
|   +-- pages
|   |   +-- index.html
|   |   +-- about.html
|   |   ...

您的pages文件夹中的文件可以通过以下方式访问:localhost:3000/pages/index.html

此外,您还没有定义路径/index.html,express正在呈现错误。