为什么我不能像在本地主机上那样查看我的主页

Why can't I view my home page like I can on a local host

我正在尝试使用 express、node 和 react 将我的网站上传到数字海洋。我可以在 localhost:3000 上查看我的网站,但是当我在 publicip:3000 上 运行 nodemon 时,我看到的只是 /root/website/src/index.html 显示在页面上。这是 server.js 文件

const express = require('express');
const app = express();

//Set port
const PORT = process.env.PORT || 3000;


//Import path
const path = require('path');

//Static files
app.use(express.static('build'));

//Server will use index.html
app.get('/*', (req, res) => {
    res.send(path.join(__dirname + '/src/index.html'));
});

app.listen(PORT, () => {
    console.log('Listening on port ${PORT}');
});

如果您使用的是 res.send(),那么它将发送文件的路径。 path.join 应该包含用逗号分隔的值,因为它将值作为字符串数组。

试试这个

如果要发送实际文件。

res.sendFile(path.join(__dirname ,"src/index.html"));