Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js + Express.js | Trying to set up a HTTPS server

I'm trying to setup a HTTPS server with Node.js and Express.js.

I'm currently trying:

const filesystem = require('fs');
const express = require('express');
const server = express();
const http = require('http').Server(server);
const https = require('https');
const io = require('socket.io')(http);
require('./routes')(server);
require('./chat-logic')(io);

// Dummy cert
const privateKey  = filesystem.readFileSync('cert/key.pem', 'utf8');
const certificate = filesystem.readFileSync('cert/cert.pem', 'utf8');

const credentials = {key: privateKey, cert: certificate};
const httpsServer = https.createServer(credentials, server);

server.use(express.static(__dirname + '/src'));
http.listen(3000, () => console.log('Listening on *3000'));
httpsServer.listen(3443, () => console.log('HTTPS on *3443'));

However, I get this error:

_tls_common.js:134
      c.context.setKey(key, passphrase);
                ^

Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt

What am I doing wrong here?

like image 766
CodeF0x Avatar asked Jan 13 '19 17:01

CodeF0x


People also ask

How can you create an https server with NodeJS?

To built an HTTPS server with nodeJs, we need an SSL (Secure Sockets Layer) certificate. We can create a self-signed SSL certificate on our local machine. Let's first create an SSL certificate on our machine first. After running this command, we would get some options to fill.

How do I start HTTPS Express server?

Go to the terminal and run the following command. After creation adds key & cert file in your code, and pass the options to the server. const express = require('express'); const https = require('https'); const fs = require('fs'); const port = 3000; var key = fs. readFileSync(__dirname + '/../certs/selfsigned.

How do I change from HTTP to HTTPS in Express?

We will perform HTTP to HTTPS redirection by creating an Express middleware function [ 1] and then, inside that function, write the redirection code that will force Express to use HTTPS. The middleware function provides access to the Express req and res objects and next function that we will need.

What is the difference between Express and Node JS?

Difference between Node.js and Express.js: Node.js is a platform for building the i/o applications which are server-side event-driven and made using JavaScript. Express.js is a framework based on Node.js for which is used for building web-application using approaches and principles of Node.js.event-driven.

How to create a web application using node terminal using express?

Firstly, install the Express framework globally using NPM so that it can be used to create a web application using node terminal. The above command saves the installation locally in the node_modules directory and creates a directory express inside node_modules.

How to start Express server with https?

4.0 you have /bin/www file, which you are going to add https here. "npm start" is standard way you start express 4.0 server. readFileSync () function should use __dirname get current directory while require () use ./ refer to current directory.

Do I need SSL for Node JS?

There typically isn't a reason to have nodejs deal with SSL, because it's just extra processing overhead which can be handled up the stack at either the ELB level or at the HTTP Proxy level. Thanks Alan; yes I've since realized that Node doesn't need to deal with SSL when AWS ELBs can be so configured.


1 Answers

Try mention you key's passphrase in this line or provide an empty string '':

const credentials = {key: privateKey, cert: certificate, passphrase: '??'};
like image 66
Badis Merabet Avatar answered Nov 06 '22 09:11

Badis Merabet