Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting client with NodeJS and Restify

I'm building a REST backend for an SPA with NodeJS, Restify and PassportJS for authentication. Everything's working except the last step, which is redirecting the client from the backends /login/facebook/callback to the home page of the application.

I've searched online and found lots of answers for ExpressJS but nothing useful for Node-Restify yet. I've managed to pick up a few snippets of code and this is what I'm attempting at the moment:

app.get('/api/v1/login/facebook/cb', passport.authenticate('facebook', { scope: 'email' }), function(req, res) {
    req.session.user = req.user._id;
    res.header('Location', '/#/home');
    res.send();
});

The response is sent but the location header is not included and the client is presented with a white screen. How do I do a proper redirect using the Node-Restify API?

like image 226
Routhinator Avatar asked Sep 04 '13 12:09

Routhinator


People also ask

What is Restify in node JS?

Restify is a framework built specifically to build REST web services in Node. js frameworks such as Express. js and Hapi. Restify manages such difficult tasks as versioning, error handling, and content negotiation.

What is the difference between Express and Restify?

Express is a minimal and flexible node. js web application framework, providing a robust set of features for building single and multi-page, and hybrid web applications; Restify: node. js REST framework specifically meant for web service APIs. A Node.

What is Restify server?

A restify server object is the main interface through which you will register routes and handlers for incoming requests.


2 Answers

Restify's Response interface now has a redirect method.

As of this writing, there's a test showing how to use it here.

The contents of that test are:

server.get('/1', function (req, res, next) {
    res.redirect('https://www.foo.com', next);
});

Many folks who use Restify are more familiar with ExpressJS. It's important to understand that (again, as of this writing) one of the three main public API differences affecting porting of Express plugins is that the res.redirect method in Restify requires you to pass next (or an InternalError is thrown). I've personally ported several modules from Express to Restify and the main API differences at first are (in Restify):

  • server.use is only for path & HTTP-method-agnostic middleware
  • res.redirect requires that you pass next
  • Some members or the Request interface are methods rather than values, such as req.path. req.path is an alias of req.getPath in Restify

I am NOT saying that under-the-hood they are similar, but that the above three things are the main obstacles to porting over Express plugins. Under-the-hood, Restify has many advantages over Express in my experience using it in both large enterprise applications and personal projects.

like image 133
james_womack Avatar answered Sep 17 '22 12:09

james_womack


You need to use redirection status code 302.

res.send(302); or res.send(302, 'your response');

like image 28
vinayr Avatar answered Sep 21 '22 12:09

vinayr