繁体   English   中英

在 Replit 上将 html 文件暴露给 express.static 中间件

[英]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 那么

https://baseurl.com

将返回 html 文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM