API 调用 Node Express 服务器后显示错误消息

showing error message on after API call to Node express server

我正在尝试根据从我的快递服务器收到的响应来处理前端错误。我正在从前端发出 POST 请求,将数据发送到快递服务器。此端点(在下面称为 /endpoint)然后向 Spotify API 和 returns 请求歌曲列表。如果由于某种原因失败了,我想在前端让用户知道。我认为我在下面编写的代码是正确的,但是当对 Spotify 的 Express 请求失败时它没有显示错误消息。

有人能发现下面的代码有什么问题吗?

快递应用

app.post('/endpoint', function(req, res){
    var options = {
       url: url,
       method: 'POST',
       json: true
     }

    request-promise(options).then(function(body){
      console.log('All good!');
    }).catch(function(err) {
       if(err) {
         console.log(err);  
         res.json(err);     
       }
    });
}

jQuery代码

$.ajax({
     type: 'POST',
     url: 'http://localhost:3000/endpoint',
     contentType: 'application/json',
     data: JSON.stringify(data),
     success: function(data) {
         console.log(data);
     },
     error: function (err) {
        console.log(err);
     }
 });

您只需要在响应中添加一个错误状态。来自快递回复 API Express 4.x

res.status(500).json({ error: 'message' });