top of page

Add HTTPS To Your Node Express Server

Writer's picture: Rakesh KumarRakesh Kumar

Updated: Mar 7, 2019


You need these things:

1. A server running on a linux distribution with root access (via SSH)

3. Express : npm install express

4. Certbot


First

To install certbot, copy-paste those lines in a terminal :

$ sudo add-apt-repository ppa:certbot/certbot

$ sudo apt-get update

$ sudo apt-get install certbot


Second, you will generate an SSL certificate with certbot :

$ certbot certonly --manual


Type your domain name(s) without the protocol part. For instance: yourdomain.com

Type Y then ENTER.


Now, don’t continue. You need to run a web server with Node & Express.

Keep your terminal opened somewhere


Now let's generate Node-JS Express app:

npm install express-generator -g

Display the command options with the -h option:

$ express -h

For example, the following creates an Express app named myapp. The app will be created in a folder named myapp in the current working directory and the view engine will be set to Pug:

$ express --view=pug myapp

Then install dependencies:

$ cd myapp
$ npm install

On MacOS or Linux, run the app with this command:

$ DEBUG=myapp:* npm start

On Windows, use this command:

> set DEBUG=myapp:* & npm start

Then load http://localhost:3000/ in your browser to access the app.

The generated app has the following directory structure:

.
├── app.js
├── bin
│   └── www
├── package.json
├── public
│   ├── images
│   ├── javascripts
│   └── stylesheets
│       └── style.css
├── routes
│   ├── index.js
│   └── users.js
└── views
    ├── error.pug
    ├── index.pug
    └── layout.pug

7 directories, 9 files

You’re almost done !

Use your favorite code editor and copy-paste this code :

Open the www file and add bellow code

const fs = require('fs'); const http = require('http'); const https = require('https'); /** * Get port from environment and store in Express. */ var port = normalizePort(process.env.PORT || '80'); app.set('port', port); /** * Create HTTP server. */ var server = http.createServer(app); const privateKey = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/privkey.pem', 'utf8'); const certificate = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/cert.pem', 'utf8'); const ca = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/chain.pem', 'utf8'); const credentials = { key: privateKey, cert: certificate, ca: ca }; const httpsServer = https.createServer(credentials, app); httpsServer.listen(443, () => { console.log('HTTPS Server running on port 443'); }); /** * Listen on provided port, on all network interfaces. */ server.listen(port); server.on('error', onError); server.on('listening', onListening); /** * Normalize a port into a number, string, or false. */ function normalizePort(val) { var port = parseInt(val, 10); if (isNaN(port)) { // named pipe return val; } if (port >= 0) { // port number return port; } return false; } /** * Event listener for HTTP server "error" event. */ function onError(error) { if (error.syscall !== 'listen') { throw error; } var bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port; // handle specific listen errors with friendly messages switch (error.code) { case 'EACCES': console.error(bind + ' requires elevated privileges'); process.exit(1); break; case 'EADDRINUSE': console.error(bind + ' is already in use'); process.exit(1); break; default: throw error; } } /** * Event listener for HTTP server "listening" event. */ function onListening() { var addr = server.address(); var bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; debug('Listening on ' + bind); } don’t forget to modify yourdomain.com


Now press enter in the terminal we have left aside. You will get success message. And now your domain is htttps certified.



5 views0 comments

Recent Posts

See All

Comments


Call

Mob: 9972946755

 

Contact:

akarotech@gmail.com

Bengaluru, Karnataka

India

 

  • facebook-square
  • twitter-bird2-square

Follow Us

 

© 2019 by Akaro Technologies (Quality Values Our Work)

bottom of page