C
C
CanVeeD2016-02-24 20:54:21
Node.js
CanVeeD, 2016-02-24 20:54:21

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

2 answer(s)
A
Anton Ulanov, 2016-02-24
@antonsr98

please show the code, index.html is the view that you want to give to the client?

I
Ivan, 2016-02-24
@LiguidCool

Actually you have black and white "res is not defined". Look for where the variable has gone.
And so - fortune-telling in the thick of some kind.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question