Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balazsbohonyi/bbd35bf0b696fbc92715932de44b6e63 to your computer and use it in GitHub Desktop.
Save balazsbohonyi/bbd35bf0b696fbc92715932de44b6e63 to your computer and use it in GitHub Desktop.
Create a redirect server in Node.js

#Create a redirect server in Node.js

'use strict'

var http = require('http');

var mappings = {
  g: 'http://www.google.com'
};

var server = http.createServer(function(req, res) {
  var alias = req.url.substring(1);

  if (!mappings[alias]) {
    res.writeHead(404);
    res.end();
    return;
  }

  res.writeHead(302, {
     location: mappings[alias]
  })
  res.end();
});

server.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment