Redirect programmatically in ZEIT Now v2.0

I wanted to perform a redirect in my Node.js serveless function as I would do using Express with res.redirect. Since the Response object is of the class http.ServerResponse one can use the method writeHead to accomplish this as follows:

module.exports = (req, res) => {
  res.writeHead(301, {Location: 'http://w3docs.com/'});
  res.end();
}

In my case I used to it redirect to the Facebook's OAUTH2 authorization URI created by simple-oauth2 as examplified here

Back to bits

Comments