繁体   English   中英

如何在节点请求错误上管道默认文件?

[英]How to pipe a default file on node request error?

我正在将nodejs与请求包一起使用。

情况:

  1. 我的前端通过HTTP GET向我的节点服务器发送文件请求
  2. 服务器检查用户是否已通过身份验证
  3. 服务器在AWS S3上请求文件,如下所示:

     request(req.query.url) .on('error', (e) => { // Some code here? res.sendFile(path.join(__dirname, '../assets/favicon.png')) }) .pipe(res) 

问题 :有时它找不到文件或无法连接到AWS S3或任何错误,我想发送一个默认文件。 我怎么能实现这个目标?

如果你开始使用try / catch而不是promises,它会不会更清洁? 喜欢:

try {
    response = request(req.query.url)
    pipe(res)
catch (e) {
    console.log("just so you can know why it failed")
    pipe(default)
}

暂无
暂无

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

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