[HTTPS] on nodejs and Centos using openssl

1. First of all, you have to create private & public key

- Install the system-specific openssl from this link.
- Create openssl.cfg http://www.flatmtn.com/article/setting-openssl-create-certificates.html
- Set the following variable : set

OPENSSL_CONF=LOCATION_OF_SSL_INSTALL\bin\openssl.cfg
Update the path : set Path=...Other Values here...;LOCATION_OF_SSL_INSTALL\bin

- Run
> openssl req -x509 -out localhost.crt -keyout localhost.key -newkey rsa:2048 -nodes -sha256

- Double click at localhost.crt



2. Create "server.js"

const { createServer } = require('https');
const { parse } = require('url');
const next = require('next');
const fs = require('fs');

const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

const httpsOptions = {
  key: fs.readFileSync('./certificates/localhost.key'),
  cert: fs.readFileSync('./certificates/localhost.crt')
};

app.prepare().then(() => {
  createServer(httpsOptions, (req, res) => {
    const parsedUrl = parse(req.url, true);
    handle(req, res, parsedUrl);
    
  }).listen(3001, err => {
    if (err) throw err;
    console.log('> Ready on https://localhost:3000');
  });
});

3. To start 
> node server.js

-------------------------------------
CentOS

> chmod 700 /etc/ssl/private
> openssl req -x509 -nodes -days 365 -newkey rsa:2048  -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt


Reference
[1] https://medium.com/@anMagpie/secure-your-local-development-server-with-https-next-js-81ac6b8b3d68
[2] https://tecadmin.net/install-openssl-on-windows/
[3] https://www.psychz.net/client/kb/en/how-to-install-ssl-certificate-on-centos-7.html

Comments

Popular posts from this blog

M5Stack ESP32

Express connect to mysql

How to debug msbuild