如何 run/view ExpressJS 服务器用于 index.html 以外的网页?

How to run/view ExpressJS server for webpages other than index.html?

所以,我想从我的 public 文件夹中 view/run/display 除 index.html 以外的网页,该文件夹有多个 html 使用 ExpressJS 和 NodeJS 的文件。每次我 运行 我的服务器,我只能查看 index.html 文件。有什么方法可以访问其他 html 文件吗?我是初学者,刚刚开始使用后端部分。 这是我的 app.js

 app=express();
const path=require('path');
const Router=express.Router();
const port=process.env.PORT||3000;
require("./db/connectdb");

const static_path=path.join(__dirname,"../../frontend/public");
app.use(express.static(static_path));

app.get('/createelection',(req,res)=>{
    console.log("Create an Election here");
});
app.listen(port,()=>{
    console.log('Server is running at port no. '+ port);
});

我的Public文件夹

Public
    -index.html
    -createelection.html
    -voterlogin.html

来自对问题的评论:

localhost:3000/createelection

默认情况下,静态模块将:

  • 如果你要求以/结尾的路径,给你index.html
  • 给你你要的文件

您要求 createelection 但文件名为 createelection.html.

使用您当前的代码,您需要请求 http://localhost:3000/createelection.html

或者,您可以让 Express 尝试为您自动完成文件扩展名。查看 the static module:

的文档

extensions: Sets file extension fallbacks: If a file is not found, search for files with the specified extensions and serve the first one found. Example: ['html', 'htm'].

设置默认为false

所以你需要:

app.use(express.static(static_path, { extensions: true }));