node.js - 如何使用 req.params 和 join 方法快速发送文件?
问题描述
我正在使用路由参数在 express.js 中创建路由。我想要一个作为参数example.com/case/firstCase
的url 。firstCase
但是,我不知道如何将 sendFile 与params
. 我想做的是追加.html
,但我认为它不起作用,因为方法join
add/
在每个元素之间用逗号分隔。换句话说,路径将是views/statics/case/firstCase/.html
这是我在 server.js 中的代码。
const express = require('express')
const app = express()
const path = require('path')
// no need to use app.use(app.router) because of that update
// function signature express.static(root, [options])
app.use(express.static('public'));
// mount root to views/statics
app.use('/', express.static('views/statics'));
const PORT = process.env.PORT || 3000;
app.listen(PORT,() => {
console.log(`Server is listening on port ${PORT}`)
});
app.get('/case',(req,res,next)=> {
res.sendFile('case.html', { root: path.join( __dirname, 'views/statics')})
})
app.get('/case/:case',(req, res)=>{
res.sendFile(path.join(__dirname, 'views/statics/case', req.params.case + '.html'));
}))
解决方案
推荐阅读
- c# - .Net Core 3.1 - 找不到方法:Microsoft.VisualBasic.Interaction.Environ(System.String)
- php - Laravel 8如何命名一个组
- mysql - OFFSET x LIMIT y 搞乱了行的顺序
- javascript - 无法读取 HTMLDivElement 处未定义的属性“id”。
- python - pip install dlib 在 Anaconda 环境中不起作用
- python - 如何将我的“打印内容”添加到文本文件中
- git - 有人可以告诉我如何使用个人访问令牌从 git 重新连接到我的远程存储库 GitHub 吗?
- r - 有没有办法从分面环绕图中省略具有 NA 值的变量?
- python - 通过交换第一个和最后一个元素、第二个和倒数第二个元素来反转列表,依此类推
- upload - Laravel 背包 - 可重复的媒体上传