[带有显示html.index的js问题?

问题描述 投票:1回答:4

这是我第一次尝试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/在我的浏览器中并显示“无法获取”帮助,请先谢谢enter image description here

enter image description here

javascript node.js express
4个回答
0
投票

请先检查控制台Express是否正在运行,然后至少触发一个api.get('pathname',callbak)这样的api使用路由然后提出问题


0
投票

您必须制作路线并渲染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'));

0
投票

您需要像这样将index option赋予index

express.static()

请记住,Express的静态渲染不会使其成为功能齐全的Web服务器来提供文件(例如apache,nginx等)。


0
投票

app.use(express.static('public', {index: 'index.html'} )); 目录位于当前工作目录(即运行publicnpm start时所在的目录)内部时,您的代码按原样工作。

如果要确保在启动脚本的任何位置都找到目录,请使用本机node src/index.js使用相对于脚本的路径。

例如,如果您的文件夹结构是这样的:

path module

您需要上一级(path):

my-project/
 |_ public/
 |   |_ index.html
 |_ src/
 |   |_ index.js
 |_ package.json

如果目录与脚本位于同一文件夹中,则可以执行

..
© www.soinside.com 2019 - 2024. All rights reserved.