[英]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.