N
N
Nikita Neskuchaev2016-10-27 12:26:10
Node.js
Nikita Neskuchaev, 2016-10-27 12:26:10

How to correctly connect an SSL certificate to a nodeJS application?

I have a working nodeJS application. It uses standard packages:
– socket.io
– fs
– request
– mysql I saw this code
on ru.stackoverflow.com . Despite the fact that I do not use redis and express, I do not understand in what format it is necessary to push the crt certificates!

var app = require('express')();
var redis = require('redis');
var server = require('https').createServer({
    key: fs.readFileSync('server.key'),
    cert: fs.readFileSync('server.crt')
},app);
var io = require('socket.io')(server);

server.listen(8890);

io.on('connection', function (socket) {

    console.log("new client connected");

    var redisClient = redis.createClient();

    redisClient.subscribe('notification');

    redisClient.on("message", function(channel, message) {
        console.log("New message: " + message + ". In channel: " + channel);
        socket.emit(channel, message);
    });

    socket.on('disconnect', function() {
        redisClient.quit();
    });

});

Bottom line: I have certificates in *.pem(.pkcs7) format. Not in *.crt. NodeJS application runs on the server, on the web (not a mobile application).
The question itself is: how do I connect my certificates in my format to a node js application.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zlatoslav Desyatnikov, 2016-10-27
@iamnikas

pem example .

var privateKey = fs.readFileSync( 'privatekey.pem' , 'utf8');
var certificate = fs.readFileSync( 'certificate.pem', 'utf8');

https.createServer({
    key: privateKey,
    cert: certificate
}, app).listen(port);

doc link

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question