简体   繁体   English

res.send 令牌然后重定向

[英]res.send token and then redirect

I have an express app that upon calling a get request immediately returns a string token, then runs a python code.我有一个快速应用程序,它在调用 get 请求时立即返回一个字符串令牌,然后运行一个 python 代码。 The python code after some time creates a json file that I need to send in another post request.一段时间后,python 代码创建了一个 json 文件,我需要在另一个 post 请求中发送该文件。

This python code may be called by different users that's why i need to assign them a token a soon as the app is called.这个 python 代码可能会被不同的用户调用,这就是为什么我需要在调用应用程序时立即为他们分配一个令牌。 In the post request i'll also be modifying the json file and then sending it back在发布请求中,我还将修改 json 文件,然后将其发回

I'm trying to do res.send the token and then res.redirect to the post request, but i know it's impossible.我正在尝试 res.send 令牌,然后 res.redirect 到发布请求,但我知道这是不可能的。 Is there any other way i could send the token or redirect to the post request?有没有其他方法可以发送令牌或重定向到发布请求?

app.get('/', (req, res) =>{
     res.send(token())
     runPython((code)=>{ 

     *takes around 10 sec*

       res.redirect('/post')}}

app.post('/post', (req, res)=>{

     *do stuff to file*

     res.sendFile()

You cannot send multiple responses back to the client separated in time.您不能在时间上分开地将多个响应发送回客户端。 You get ONE http response.你得到一个 http 响应。 So, once you've done res.send(token()) , then that http request is done.所以,一旦你完成了res.send(token()) ,那么那个 http 请求就完成了。 You can't send any more data as part of that http request.作为该 http 请求的一部分,您不能再发送任何数据。

As you describe things, here are some of your options:在您描述事物时,以下是您的一些选择:

  1. Combine both into one response.将两者合并为一个响应。 Wait to send the token until you have the python response too and then send them both in one JSON response.等待发送令牌,直到您也有 python 响应,然后将它们都发送到一个 JSON 响应中。 You won't be able to do a res.redirect() if you're also sending data back.如果您还要发回数据,您将无法执行res.redirect() So, you could send the redirect location back in the JSON and have the client manually do the redirect (by just setting window.location to a new URL).因此,您可以将重定向位置发送回 JSON,并让客户端手动执行重定向(只需将window.location设置为新 URL)。 Presumably, there is client-side Javascript on the receiving end of this anyway since some form of code has to receive the token anyway to do something useful with it.据推测,无论如何,在接收端都有客户端 Javascript,因为某种形式的代码无论如何都必须接收令牌才能用它做一些有用的事情。

  2. Use websocket/socket.io connection for subsequent server-initiated communication.使用 websocket/socket.io 连接进行后续服务器启动的通信。 Have the client connect a webSocket or socket.io connection.让客户端连接 webSocket 或 socket.io 连接。 You can then response with the token and then later when the python has finished, you can send additional data over the websocket or socket.io connection.然后,您可以使用令牌进行响应,然后在 Python 完成后,您可以通过 websocket 或 socket.io 连接发送其他数据。 This will require additional code to be able to associate a particular websocket/socket.io connection with the client that made this request so you can tell which websocket/socket.io connection for this request to send a notification over.这将需要额外的代码才能将特定的 websocket/socket.io 连接与发出此请求的客户端相关联,以便您可以判断此请求要通过哪个 websocket/socket.io 连接发送通知。

  3. Client-side polling for completion of python operation.用于完成python操作的客户端轮询。 Have the server send back the token and also send it some sort of request ID and then the client can poll the server every few seconds to ask the server if that python operation for that request ID is now done.让服务器发回令牌并向其发送某种请求 ID,然后客户端可以每隔几秒钟轮询服务器以询问服务器该请求 ID 的 Python 操作现在是否已完成。 When it gets a response from the server that the python is now done, then the client can manually redirect itself to /post to fetch the final data.当它从服务器得到 python 已经完成的响应时,客户端可以手动将自己重定向到/post以获取最终数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM