Create nodejs https server
HTTPS is a communication protocol, widely used in modern web applications. Web applications like shopping carts, social networks, and other portals etc, use HTTPS protocol to transfer secure information over the Internet. HTTPS uses TLS/SSL protocols. In a previous article, you learned Nodejs twitter login with passportjs. In this article, you will learn, how to create nodejs https server on localhost. OpenSSL is used to create SSL certificates.
In this article, following tasks are performed.
1. Download and install OpenSSL
2. Generate privatekey.pem and certificate.pem files on localhost
3. Generate a NodeJS, ExpressJS application
4. Add HTTPS in a NodeJS application
Download and install OpenSSL for nodejs https server
To generate SSL certificates, you need to install OpenSSL, Visit OpenSSL download page, download and install it. On Windows 64-bit operating system, you can install Win64 OpenSSL v1.1.0h Light.
Generate SSL certificates for nodejs https server
On Windows operating system, open command line and choose option Run as administrator. Type this command
cd C:\OpenSSL-Win64\bin
To generate certificate.pem and privatekey.pem files, run the commands given below.
openssl genrsa -out privatekey.pem 1024
while pressing Enter key you can choose default values.
openssl req -new -key privatekey.pem -out certrequest.csr -config C:\OpenSSL-Win64\bin\openssl.cfg
The next command to generate SSL certificates for nodejs https server.
openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem
The commands above will create privatekey.pem and certificate.pem in the bin directory.
Generate certificate files on your computer and copy to keys directory before running the application
OpenSSL commands to generate SSL certificates
openssl genrsa -out privatekey.pem 1024 openssl req -new -key privatekey.pem -out certrequest.csr -config C:\OpenSSL-Win64\bin\openssl.cfg openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem
Install NodeJS and NPM
To install NodeJS on your computer, visit nodejs.org. Download latest NodeJS version, run the installer. With NodeJS installation, NPM or Node Package Manager is also installed. It is used to manage and install dependencies and packages.
Install express generator command line tool
After NodeJS installation, install express-generator. This command line utility is used to generate a nodejs, expressjs app.
npm install express-generator -g
Generate an express application using express tool
Create an expressjs application nodejs-https-server. Pug is a template engine, used in ExressJS applications.
express nodejs-https-server --view=pug
Next, install required packages and dependencies.
cd nodejs-https-server && npm install
You can run the application by executing the command below.
SET DEBUG=nodejs-https-server:* & npm start
Install nodemon module
Nodemon module watches file changes in a directory. It restarts the nodejs application if any change in a file is detected. Otherwise, each time you have to stop and restart nodejs application manually.
npm install nodemon --save
Run application with nodemon
To run the application, open the command line, navigate to project root directory and type.
nodemon
Create keys directory
Create a directory named, keys in the project root. Copy privatekey.pem and certificate.pem file from C:\OpenSSL-Win64\bin to this directory.
Add https to bin/www file
Open www file in bin directory. Add following require the statement in the file.
var https = require('https'); var fs = require('fs');
Add https options
Add https options and fetch certificates using fs.readFileSync method from keys directory.
var httpOptions = { key: fs.readFileSync("keys/privatekey.pem"), cert: fs.readFileSync("keys/certificate.pem") }
In www file, remove the line
var server = http.createServer(app);
and add
var server = https.createServer(httpOptions, app);
In https.create method httpOptions and app parameters are passed. Https server is created.
Run the application
If nodemon is not already running, run it on command line.
nodemon
Generate certificate files on your computer and copy to keys directory before running the application
Open homepage with nodejs https server
If nodemon is running then, open browser and type URL
https://localhost:3000
While accessing this site first time on Google Chrome, you will see a screen like this.
Click on Advanced, and then click on Proceed to localhost (unsafe). You can view the homepage as below.
Open users page with nodejs https server
Next open browser and type URL using https protocol.
https://localhost:3000/users
You can view user’s page with https.
Summary
In this article, you learned to create a nodejs https server on localhost using OpenSSL. First, OpenSSL was downloaded and installed on the local computer. Certificates were generated using Windows command line. A Nodejs, Express application is generated using express command line tool. A nodejs https server is created. Private and public keys were added as options.
You can find the source code of this article on GitHub. You can clone or download from this link.
Please leave your feedback. Subscribe to our newsletter, like us on Facebook or follow us on twitter to stay updated on latest articles.
Related Articles:
- Nodejs File Upload With MongoDB – Photo Gallery
- NodeJs user registration tutorial
- NodeJS send email tutorial
- NodeJS MongoDB RESTful API
- Nodejs MongoDB tutorial
Previous Article:
Next Article: