Rss Feed Tweeter button Facebook button Technorati button Reddit button Linkedin button Webonews button Delicious button Digg button Flickr button Stumbleupon button Newsvine button

A Waage Blog

Ruby, Rails, Life

Node.js and RESTify Server with both HTTP and HTTPS

with one comment

Here’s a quick guide on setting up a Node.js server using the restify module that accepts BOTH http and https connections.

We want to re-use as much code as possible because the two servers (HTTP and HTTPS) are identical besides the protocol.

In the example, we created a self-signed SSL certificate. If you need help on that, check out this quick guide on how to set up a self-signed SSL certificate on Ubuntu.

var restify = require('restify');
var fs = require('fs');

// Setup some https server options
var https_options = {
  key: fs.readFileSync('/etc/ssl/self-signed/server.key'),
  certificate: fs.readFileSync('/etc/ssl/self-signed/server.crt')

// Instantiate our two servers
var server = restify.createServer();
var https_server = restify.createServer(https_options);

// Put any routing, response, etc. logic here. This allows us to define these functions
// only once, and it will be re-used on both the HTTP and HTTPs servers
var setup_server = function(app) {
        function respond(req, res, next) {
           res.send('I see you ' +;

        // Routes
        app.get('/test/:name', respond);

// Now, setup both servers in one step

// Start our servers to listen on the appropriate ports
server.listen(80, function() {
  console.log('%s listening at %s',, server.url);

https_server.listen(443, function() {
  console.log('%s listening at %s',, https_server.url);

Written by Andrew Waage

February 27th, 2012 at 4:05 pm

Posted in Node.js

One Response to 'Node.js and RESTify Server with both HTTP and HTTPS'

Subscribe to comments with RSS or TrackBack to 'Node.js and RESTify Server with both HTTP and HTTPS'.

  1. [...] you can use the files server.key and server.crt in your web server setups. See my post on Setting up Node.js with to accept HTTP and HTTPS connections for a quick example of using this SSL certificate with [...]

Leave a Reply