Node.js:响应是否发送给正确的用户?

发布于 2024-12-03 15:23:05 字数 519 浏览 1 评论 0原文

假设我有一个简单的代码,它用 .XML 文件进行响应。

app.post '/incoming', (req,res) ->
    console.log "Hello, incoming call!"
    message = req.body.Body
    from = req.body.From

    sys.log "From: " + from + ", Message: " + message 

    test = "hello"
    r = new Twiml.Response()
    r.append(new Twiml.Say('Hello, there!' + test + ' Enter your ATM card PIN'))
    console.log(r.toString())

    res.send r.toString()

如果同时发出 2 个请求,有可能得到错误的响应吗?我问这个问题是因为我不完全理解异步是如何工作的以及这是否正在做我想要它做的事情。

谢谢

Let's say I have this simple code that responds with a .XML file.

app.post '/incoming', (req,res) ->
    console.log "Hello, incoming call!"
    message = req.body.Body
    from = req.body.From

    sys.log "From: " + from + ", Message: " + message 

    test = "hello"
    r = new Twiml.Response()
    r.append(new Twiml.Say('Hello, there!' + test + ' Enter your ATM card PIN'))
    console.log(r.toString())

    res.send r.toString()

Is it possible that if 2 requests come at the same time, one gets the wrong response? I'm asking this because I don't fully understand how async works and if this is doing what I want it to do.

Thanks

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

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

发布评论

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

评论(2

凉城凉梦凉人心 2024-12-10 15:23:05

如果您想知道响应发送到哪个客户端,请从正文中删除除了与 res 交互的代码之外的所有代码,如下所示:

app.post '/incoming', (req,res) ->
    res.send r.toString()

现在您可以看到您发送的 r.toString() 到正确的响应。

是否有可能如果同时有2个请求,

即使 2 个请求同时到来,javascript 也是单线程和阻塞的,因此永远不会有任何竞争条件。

If you want to know which client the response goes to, remove all code from the body apart from code that interacts with res like so :

app.post '/incoming', (req,res) ->
    res.send r.toString()

Now you can see that your sending r.toString() to the correct response.

Is it possible that if 2 requests come at the same time,

Even if 2 requests come at the same time, javascript is single threaded and blocking so there are never any race conditions.

三生池水覆流年 2024-12-10 15:23:05

你问的实际上是一个关于 CoffeeScript 范围的问题。当您有一个函数时,例如

(req, res) ->

在该函数内,您可以放心 reqres 将始终指向传递给该函数的对象。唯一的例外是如果您有一个具有相同参数名称的嵌套函数。例如,

(req, res) ->
  setTimeout ((req, res) -> console.log req, res), 1

当使用任何值调用外部函数时,都会显示 undefined, undefined,因为传递给 setTimeout 的函数有自己的 reqres 参数遮蔽外部 reqres

显然你没有这样做(而且你不应该这样做),所以你不必担心。您的服务器每秒可以被 ping 数万次,并且每个 reqres 将保持不同。

What you're asking is really a question about CoffeeScript scope. When you have a function, e.g.

(req, res) ->

then within that function, you can be assured that req and res will always point to the objects that were passed in to the function. The only exception is if you have a nested function with the same argument names. For instance,

(req, res) ->
  setTimeout ((req, res) -> console.log req, res), 1

will display undefined, undefined when the outer function is called with any values, because the function passed to setTimeout has its own req and res arguments that shadow the outer req and res.

Obviously you're not doing that (and well you shouldn't), so you needn't worry. Your server can be pinged tens of thousands of times per second, and each req and res will remain distinct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文