Javascript 如何将API响应作为JSON对象发送到客户端?

Javascript 如何将API响应作为JSON对象发送到客户端?,javascript,node.js,express,Javascript,Node.js,Express,响应是来自字典API的JSON对象。响应已成功登录到控制台。如何将响应中包含的JSON对象发送回客户端 router.get('/dictionary_test', (req, res, next) => { const lookup = dict.find("apple"); lookup.then(res => { console.log(res); }, (err) => { console.log(err); }) }); 当您有数

响应是来自字典API的JSON对象。响应已成功登录到控制台。如何将响应中包含的JSON对象发送回客户端

router.get('/dictionary_test', (req, res, next) => {
  const lookup = dict.find("apple");
  lookup.then(res => {
    console.log(res);
  },
  (err) => {
    console.log(err);
  })
});
当您有数据时,只需使用res.json。并确保您没有通过意外定义另一个同名的本地参数来隐藏父res。请注意对结果的更改,以便它不会与res冲突:

您正在跟踪res变量。您可以这样做。thendictResult=>res.jsondictResult
router.get('/dictionary_test', (req, res, next) => {
  const lookup = dict.find("apple");
  lookup.then(result => {
    console.log(result);
    res.json(result);
  }, err => {
    console.log(err);
    res.sendStatus(500);
  });
});