从特快专递路线中将字符串发送回客户端

问题描述

我试图将返回的字符串(一旦计算出)发送回去,因为它是从express上的发布路由发回给客户的

在后端,我有一条发布路线:

router.post('/',async (req,res) => { 
 try {
  // Here I do some computation on a succesion that generates a new string everytime
  const foo = 'string that changes and I want to send to client' 
  
 } catch (error) {
   res.status(500).send(error)
 }
})

然后在客户端上,我正在使用axios发送数据以进行发布

(async () => {
  try {
    await axios.post('/api/send/',{ data })
  } catch (error) {
    console.log(error)
  }
})()

发送帖子请求后,是否有办法在发生路由后从该路由取回数据?

我尝试从发布路由中使用res.send(),但这只会使后端的功能失败。

非常感谢。

解决方法

router.post('/',(req,res) => { 
try {
    // Here I do some computation on a succesion that generates a new string everytime
    const foo = 'string that changes and I want to send to client' 
    res.send(foo);
} catch (error) {
      res.status(500).send(error)
  }
})

这足够了。如果不是,则需要详细说明错误消息是什么。