Skip to content

Instantly share code, notes, and snippets.

@leommoore
Created April 1, 2014 10:30
Show Gist options
  • Save leommoore/9911555 to your computer and use it in GitHub Desktop.
Save leommoore/9911555 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