这是我第一次尝试Express js,并且我试图显示index.html ho在“公共”文件夹中,我不知道问题出在哪里在副服务器上,我有:
const express = require('express');
const app = express();
app.listen(3000, () => console.log('listening at 3000'));
app.use(express.static('public'));
并且我用git bash重新加载服务器;然后将index.html放在“ public”目录中,然后重新运行服务器,将http://localhost:3000/在我的浏览器中并显示“无法获取”帮助,请先谢谢
请先检查控制台Express是否正在运行,然后至少触发一个api.get('pathname',callbak)这样的api使用路由然后提出问题
您必须制作路线并渲染index.html
const express = require('express');
const app = express();
app.use(express.static('public'));
app.route('/',function () {
res.sendFile('index.html');
});
app.listen(3000, () => console.log('listening at 3000'));
app.use(express.static('public', {index: 'index.html'} ));
目录位于当前工作目录(即运行public
或npm start
时所在的目录)内部时,您的代码按原样工作。
如果要确保在启动脚本的任何位置都找到目录,请使用本机node src/index.js
使用相对于脚本的路径。
例如,如果您的文件夹结构是这样的:
path
module
您需要上一级(path
):
my-project/
|_ public/
| |_ index.html
|_ src/
| |_ index.js
|_ package.json
如果目录与脚本位于同一文件夹中,则可以执行
..