【发布时间】:2021-07-19 10:13:58
【问题描述】:
当我在 Nodejs 上托管我的前端时,我使用 app.use(express.static(path.join(__dirname, "public")));。但是,这使我将 HTML 文件的名称放在 URL 的末尾;例如,localhost:3000/index.html。
如何让它在基本 URL 上显示前端,在本例中为“localhost:3000/”。我附上了我的 Nodejs 代码的一部分。如有任何帮助,我将不胜感激,并在此先感谢您。
var path = require('path');
var express = require('express');
const app = express();
const port = process.env.PORT || '3000';
app.use(express.json());
app.use((req, res, next) => {
console.log(req.path);
next();
});
app.use(express.static(path.join(__dirname, "public")));
app.listen(port, () => console.log("Server is ready"));
【问题讨论】:
-
那么你在后端服务器上使用 index.html 作为首页吗?
-
你的前端是什么?是框架还是更多的html模板?