[英]Exposing html file to express.static middleware on Replit
On Replit, rendering the html file using res.sendFile
inside a app.get
works perfectly fine, AND I am able to add logos, styles, and js logic file by passing in express.static
middleware.在 Replit 上,使用 app.get 中的res.sendFile
呈现app.get
文件效果非常好,我可以通过传入express.static
中间件来添加徽标、styles 和 js 逻辑文件。 BUT when I try to also include the html as a static file passed to express.static
middleware, the page does not render.但是当我尝试将 html 作为传递给express.static
中间件的 static 文件时,页面不会呈现。
Here's the replit: https://replit.com/@yanichik/NodeJSandExpressFullCourseFCC#02-express-tutorial/app.js这是 replit: https://replit.com/@yanichik/NodeJSandExpressFullCourseFCC#02-express-tutorial/app.js
Renders as expected when html passed in with res.sendFile
:当 html 通过res.sendFile
传入时按预期呈现:
const express = require('express'); const express = require('快递'); const path = require('path'); const path = require('路径');
const app = express(); const app = express();
// setup static & middleware // static -> file that server does NOT have to change app.use(express.static(path.resolve(__dirname, './public'))) // 设置 static 和中间件 // static -> 服务器不必更改的文件 app.use(express.static(path.resolve(__dirname, './public')))
app.get('/', (req, res) => { res.sendFile(path.resolve(__dirname, './navbar-app/index.html')) }) app.get('/', (req, res) => { res.sendFile(path.resolve(__dirname, './navbar-app/index.html')) })
app.all('*', (req, res) => { res.status(404).send('Resource not found.') }) app.all('*', (req, res) => { res.status(404).send('找不到资源。') })
app.listen(5000, () => { console.log('Server listening on port 5000...') }) app.listen(5000, () => { console.log('服务器侦听端口 5000...') })
module.exports = app; module.exports = 应用程序;
Now, DOES NOT render as expected when html passed in with express.static
middleware:现在,当 html 通过express.static
中间件传递时,不会按预期呈现:
const express = require('express'); const express = require('快递'); const path = require('path'); const path = require('路径');
const app = express(); const app = express();
// setup static & middleware // static -> file that server does NOT have to change app.use(express.static(path.resolve(__dirname, './public'))) // 设置 static 和中间件 // static -> 服务器不必更改的文件 app.use(express.static(path.resolve(__dirname, './public')))
// app.get('/', (req, res) => { // // app.get('/', (req, res) => { //
res.sendFile(path.resolve(__dirname, './navbar-app/index.html')) // }) res.sendFile(path.resolve(__dirname, './navbar-app/index.html')) // })app.all('*', (req, res) => { res.status(404).send('Resource not found.') }) app.all('*', (req, res) => { res.status(404).send('找不到资源。') })
app.listen(5000, () => { console.log('Server listening on port 5000...') }) app.listen(5000, () => { console.log('服务器侦听端口 5000...') })
module.exports = app; module.exports = 应用程序;
You have to specifically request for the statically exposed files like so:您必须像这样专门请求静态公开的文件:
https://baseURL.com/navbar-app/index.html https://baseURL.com/navbar-app/index.html
When you comment out get routes.当您注释掉获取路线时。
If you have your get route uncomented route then如果你有你的 get route uncommented route 那么
https://baseurl.com https://baseurl.com
Will return the html file将返回 html 文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.