![](/img/trans.png)
[英]Express middleware for express.static() not routing correctly and throwing next() is not a function
[英]Exposing html file to express.static middleware on Replit
在 Replit 上,使用 app.get 中的res.sendFile
呈現app.get
文件效果非常好,我可以通過傳入express.static
中間件來添加徽標、styles 和 js 邏輯文件。 但是當我嘗試將 html 作為傳遞給express.static
中間件的 static 文件時,頁面不會呈現。
這是 replit: https://replit.com/@yanichik/NodeJSandExpressFullCourseFCC#02-express-tutorial/app.js
當 html 通過res.sendFile
傳入時按預期呈現:
const express = require('快遞'); const path = require('路徑');
const app = express();
// 設置 static 和中間件 // static -> 服務器不必更改的文件 app.use(express.static(path.resolve(__dirname, './public')))
app.get('/', (req, res) => { res.sendFile(path.resolve(__dirname, './navbar-app/index.html')) })
app.all('*', (req, res) => { res.status(404).send('找不到資源。') })
app.listen(5000, () => { console.log('服務器偵聽端口 5000...') })
module.exports = 應用程序;
現在,當 html 通過express.static
中間件傳遞時,不會按預期呈現:
const express = require('快遞'); const path = require('路徑');
const app = express();
// 設置 static 和中間件 // static -> 服務器不必更改的文件 app.use(express.static(path.resolve(__dirname, './public')))
// app.get('/', (req, res) => { //
res.sendFile(path.resolve(__dirname, './navbar-app/index.html')) // })app.all('*', (req, res) => { res.status(404).send('找不到資源。') })
app.listen(5000, () => { console.log('服務器偵聽端口 5000...') })
module.exports = 應用程序;
您必須像這樣專門請求靜態公開的文件:
https://baseURL.com/navbar-app/index.html
當您注釋掉獲取路線時。
如果你有你的 get route uncommented route 那么
將返回 html 文件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.