1

I am using Express.js in my project. When I submit a form, a function is executed and what I would like is that at the end of the function, it changes the page from index to results.

io.on('connection', function(socket){
  socket.on('query', function(qCity,qState,qLowPrice,qHighPrice, req, res){

I want it to redirect here:

  }); // end of socket.on query function
}); // end of io.on

I've read on some posts about using middleware but still not sure it's exactly what I want.

1
  • um how do you ping that socket? is that on server or on userside? what is req and res? Commented Apr 12, 2018 at 19:38

2 Answers 2

6

use res.redirect

io.on('connection', function(socket){
    socket.on('query', function(qCity,qState,qLowPrice,qHighPrice, req, res){
    res.redirect('http://yourdomain.com/result')

    });
Sign up to request clarification or add additional context in comments.

5 Comments

Ahh 30 seconds to slow: )
This is what you want. You can learn more by reading the Express.js API documentation found at: expressjs.com/en/4x/api.html#res
When I try this it errors with res.redirect('/results.ejs')TypeError: Cannot read property 'redirect' of undefined
@sewersama do you have a page called result ?
Yes I do, I think the error is related to res in someway because if I'm reading it correctly it thinks res is undefined inside of the function so maybe i have to make it global somehow or something.
3

You should just be able to use redirect in express like this:

res.redirect('your/path.html');

1 Comment

For some reason it errors with res.redirect('/results.ejs')TypeError: Cannot read property 'redirect' of undefined. Any idea about this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.