Node.js HTTPS server with self-signed certificate creation on openssl 0.9.8zh with node.js 7.10.0

I couldn’t find a concise guide to setting this up quickly so thought it was worth a post. To quickly get something working and create a https server using the above versions of openssl and node.js, do the following:

 Generate self-signed server certificate with no password

sudo openssl req -x509 -newkey rsa:2048 -keyout ./csr.pem -out server.crt -days 3001 -nodes

Use this node.js code to setup a server quickly

const https = require('https');
const fs = require('fs');

const options = {
key: fs.readFileSync('csr.pem'),
cert: fs.readFileSync('server.crt')
};

https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);

Go to https://localhost:8000 and accept the certificate, you should see ‘hello world’

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s