![](/img/trans.png)
[英]express.static() not serving files from public folders in router paths that are not "/"
[英]Express.static is not serving login page
我设置主页是login.html,但是为什么主页是index.html?
我试图删除 index.html。 事实证明 web 显示 login.html 所以我想知道。
应用程序.js
const express = require('express')
const bodyParser = require('body-parser');
const path = require('path');
const app = express()
const PORT = process.env.PORT || 3000
express.static('public')
app.use(express.static('public'))
app.use(bodyParser.urlencoded({extended : true}));
app.use(bodyParser.json());
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname,'/public/login.html'))
})
app.listen(PORT, () => {
console.log(`Server is running on port : ${PORT}`)
})
公共文件夹
public
---> css
---> img
index.html
login.html
关于serve-static ,文档说:
默认情况下,此模块将发送“index.html”文件以响应对目录的请求。 要禁用此设置 false 或提供新索引,请按首选顺序传递字符串或数组。 http://expressjs.com/en/resources/middleware/serve-static.html
在您的示例中,您可以通过将"index"选项设置为"false"来禁用此默认行为:
app.use(express.static("public", { index: false }));
还有一个传递数组的选项(如文档中所述):
app.use(express.static("public", { 'index': ['login.html', 'index.htm'] }));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.