Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias
Last active August 29, 2015 14:03
Show Gist options
  • Save JosePedroDias/de5d2cad751f32a24c04 to your computer and use it in GitHub Desktop.
Save JosePedroDias/de5d2cad751f32a24c04 to your computer and use it in GitHub Desktop.
HTTPS server for tests

prepare structure

mkdir files certs
npm install express

create credentials

fire the console:

openssl genrsa -out creds/key.pem 1024 
openssl req -new -key creds/key.pem -out creds/cert_req.csr
openssl x509 -req -in creds/cert_req.csr -signkey creds/key.pem -out creds/cert.pem

Common Name is where you'll want to put the domain name you'll be using to access your site

create the server

create a file named serve.js with:

var fs      = require('fs');
var http    = require('http');
var https   = require('https');
var express = require('express');


var key  = fs.readFileSync('./creds/key.pem',  'utf8');
var cert = fs.readFileSync('./creds/cert.pem', 'utf8');


var credentials = {key:key, cert:cert};


var app = express();

app.configure(function(){
    app.use('/', express.static(__dirname + '/files'));
});


//var srv1  = http.createServer(app);
//srv1.listen( 80);

var srv2 = https.createServer(credentials, app);
srv2.listen(443);

add some content to serve in the files dir.

Ex: files/index.html:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		
		<title>yay</title>
	</head>
	
	<body>
		hi
	</body>
</html>

serve them with express

sudo node serve.js

(because 443 < 1024)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment