Answer the question
In order to leave comments, you need to log in
How to redirect in express?
Hello, I have a question that I've been looking for an answer to for a couple of days now. I am writing a Node.Js chat and I am facing a problem. When authorizing, the client sends a login \ password, the server checks the data in MongoDB, and if everything is correct, the server should redirect the client to the next page. I read the Express 4 documentation and it says: res.redirect('/rooms'); , and in theory there will be a redirect to /rooms, but the server gave an error: res is not defined. I tried to introduce a def variable into:
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
def = res;
})
and then def.redirect( ); but the error is: Error: Can't set headers after they are sent. I don't know how to fix this and I'm asking for your help, thanks in advance!
Answer the question
In order to leave comments, you need to log in
please show the code, index.html is the view that you want to give to the client?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question