Express Return Post Info To Be Processed By Another Function
I have this code: app.post('/pst', function(req, res) { var data = req.body.convo; res.render('waiting.ejs'); //ADDED THIS myFunc(data).then
Solution 1:
HTTP is a request/response protocol. A client makes a request and gets one and only one response back. So, you can never call res.render()
twice on the same request.
Your problem is really defined by this desired sequence:
- Client sends request
- Server starts to process request
- Client shows progress
- Server finishes processing request
- Client shows final result
There are a number of ways to accomplish that:
Client uses Ajax instead of form post to send request
- Client submits form via Ajax instead of form post
- Client puts up progress on the page using DOM manipulation (changing of the current page contents, usually showing a visual overlay of some type)
- Server works on request, not returning anything yet
- Server finishes request and returns response to client with one
res.render()
- Client either inserts the returned content into the current page or issues a
window.location = xxxx
to change the current page to show new URL containing new contents.
Form post response that uses webSocket/socket.io to get final results
- Client submits form
- Server returns immediately response page that shows progress/waiting UI and that page also connects a webSocket or socket.io connection to the server
- Server works on request
- Server accepts webSocket or socket.io connection
- Server finishes request and sends some type of result over webSocket/socket.io connection to the correct client
- Client receives response over webSocket/socket.io and either updates the contents of the current page or changes page to a new URL
All done via webSocket/socket.io
- Client loads original page
- Client establishes webSocket/socket.io connection to server
- Client sends form data to server over webSocket/socket.io connection
- Client puts up progress/waiting overlay
- Server starts processing request
- Server finishes processing request
- Server sends response from request back to client over webSocket/socket.io connection for that client.
- Client receives response over webSocket/socket.io connection and either updates the contents of the current page or changes page to a new URL
Post a Comment for "Express Return Post Info To Be Processed By Another Function"