使用expressJS,res和req是否传递给函数?

发布于 2024-12-04 08:04:52 字数 600 浏览 2 评论 0原文

我正在使用 CoffeeScript,请注意:

searchResults = (err, data)->
  res.write 'hello'
  console.log data
  console.log 'here'
  return


exports.search = (req, res) ->
  res.writeHead 200, {'Content-Type': 'application/json'}
  location = req.param 'location'
  item = req.param 'item'

  geoC = googlemaps.geocode 'someaddress', (err, data) ->
      latLng = JSON.stringify data.results[0].geometry.location

      myModule.search latLng, item, searchResults

      return
  return

searchResults 函数不知道 res,那么我如何将数据返回到浏览器?

I'm using CoffeeScript, just a heads up:

searchResults = (err, data)->
  res.write 'hello'
  console.log data
  console.log 'here'
  return


exports.search = (req, res) ->
  res.writeHead 200, {'Content-Type': 'application/json'}
  location = req.param 'location'
  item = req.param 'item'

  geoC = googlemaps.geocode 'someaddress', (err, data) ->
      latLng = JSON.stringify data.results[0].geometry.location

      myModule.search latLng, item, searchResults

      return
  return

The searchResults function doesn't know about res, so how can I return data to the browser?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃颗糖壮壮胆 2024-12-11 08:04:52

这是一个很常见的场景。一种选择是在 exports.search 内部定义 searchResults,但是 exports.search 可能会变得笨拙。

res 不是参数时,以使用 res 的方式定义 searchResults 是没有意义的。但是您可能不愿意使用带有多个参数的函数,当您有多个需要访问相同状态的回调时,这可能会导致重复的代码。一个好的选择是使用单个哈希来存储该状态。在这种情况下,您的代码可能类似于

searchResults = (err, data, {res}) ->
  ...

exports.search = (req, res) ->
  res.writeHead 200, {'Content-Type': 'application/json'}
  location = req.param 'location'
  item = req.param 'item'
  state = {req, res, location, item}

  geoC = googlemaps.geocode 'someaddress', (err, data) ->
      state.latLng = JSON.stringify data.results[0].geometry.location
      myModule.search state, searchResults
      return
  return

注意 myModule.search 现在仅采用 state 哈希值和回调;然后,它将 state 哈希作为第三个参数传递给该回调 (searchResults),该回调使用解构参数语法从哈希中提取 res

It's a pretty common scenario. One option is to define searchResults inside of exports.search, but then exports.search might get unwieldy.

It doesn't make sense for searchResults to be defined in such a way that it uses res when res isn't an argument. But you may be reluctant to have functions with several arguments, which can lead to repetitive code when you have several callbacks that need to access the same state. One good option is to use a single hash to store that state. In this case, your code might look something like

searchResults = (err, data, {res}) ->
  ...

exports.search = (req, res) ->
  res.writeHead 200, {'Content-Type': 'application/json'}
  location = req.param 'location'
  item = req.param 'item'
  state = {req, res, location, item}

  geoC = googlemaps.geocode 'someaddress', (err, data) ->
      state.latLng = JSON.stringify data.results[0].geometry.location
      myModule.search state, searchResults
      return
  return

Notice that myModule.search now only takes the state hash and a callback; it then passes the state hash as the third argument to that callback (searchResults), which pulls res out of the hash using the destructuring argument syntax.

任谁 2024-12-11 08:04:52

标准绑定就可以了。

myModule.search latLng, item, searchResults.bind(null, res)

...

searchResults = (res, err, data)->
  res.write 'hello'
  console.log data
  console.log 'here'
  return

A standard bind will do.

myModule.search latLng, item, searchResults.bind(null, res)

...

searchResults = (res, err, data)->
  res.write 'hello'
  console.log data
  console.log 'here'
  return
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文