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?
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.
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.
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.
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.
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.
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.
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.
Try mention you key's passphrase
in this line or provide an empty string ''
:
const credentials = {key: privateKey, cert: certificate, passphrase: '??'};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With