node.js - 当id包含“/”时如何通过id使用GET
问题描述
我正在使用 Node.js 和 aws-sdk 构建一个 API,所以我有一个GET函数来通过ID或KEY下载文件
export const downloadFile = async(req: Request, res: Response) => {
const params = {
Bucket: AWS_S3.Bucket,
Key: req.params.key
};
res.setHeader('Content-Disposition', 'attachment');
s3.getObject(params)
.createReadStream()
.on('error', error => {
return res.status(500).json({
message: 'Ocurrio un error al descargar el archivo',
error
});
}).pipe(res);
}
问题在于键的名称,因为包含/并且 Node.js 认为这是一个新路由。
例子:
const URL_API = 'http://localhost:3000/api';
const KEY = '/mendozarq/images/example.jpg'
const URL = `${URL_API}/download/${KEY}`;
最后我得到了这个错误:
Cannot GET /api/download//mendozarq/images/example.jpg
解决方案
推荐阅读
- android - 应用程序在后台时的参与时间计算
- python - PIL中“P”和“L”模式下的图像有什么区别?
- java - event.consume() 方法在 javafx 中不起作用
- php - CI中的多个mysql查询
- javascript - javascript中的await关键字会减慢应用程序的速度吗?
- amazon-web-services - 如何在不影响数据的情况下删除 DynamoDb 表中的范围键列?
- php - 如何在不使用 php artisan 的情况下创建 Laravel 开发服务器
- php - 如何在 Wordpress 的其他地方显示产品的价格
- ruby-on-rails - 从 rails 4.2 迁移到 5 问题需要降级 ActiveRecord 版本才能使用 1 个 gem
- javascript - 如何用这个解构数组