I am trying to create a secure node.js server to use with my site that is using ssl (https).
const crypto = require(\'crypto\'),
fs = require(\"fs\"),
I have worked on the https secure with the ssl here is the working code for making the https and http
var fs = require('fs');
var http = require('http');
var https = require('https');
var debug = require('debug')('expressapp');
var app = require('../app');
var CONSTANTS = require('../config/CONSTANTS.js');
var AWS = require('aws-sdk');
var certificate =fs.readFileSync('ssl/server.crt',{encoding:'utf8'},function(err, data ) {
console.log( data );});
var privateKey = fs.readFileSync('ssl/server.key',{encoding:'utf8'},function(err, data ) {
console.log( data );});
var credentials = {
key: privateKey,
cert: certificate,
rejectUnauthorized:false
};
// UNCOMMENT THIS LINE AFTER INSTALLING CA CERTIFICATE
//credentials.ca = fs.readFileSync('ssl/server.crt', 'utf8');;
var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
httpServer.listen(CONSTANTS.PORT.HTTP, function() {
console.log('HTTP server listening on port ' + CONSTANTS.PORT.HTTP);
}) ;
httpsServer.listen(CONSTANTS.PORT.HTTPS, function() {
console.log('HTTPS server listening on port ' + CONSTANTS.PORT.HTTPS);
});
this setup allowed me to connect to my socket.io server ssl (HTTPS/WSS)
http=require('https'),io=require('socket.io'),fs=require('fs');
var privateKey = fs.readFileSync('ssl/nginx.key');
var certificate = fs.readFileSync('ssl/nginx.crt');
var options = {key: privateKey,cert: certificate};
var server = http.createServer(options);
server.listen(3000);
io = io.listen(server);
HTTPS implementation was re-done in Node.JS 0.4. See the corresponding docs at nodejs.org.
Example from the docs:
var tls = require('tls');
var fs = require('fs');
var options = {
key: fs.readFileSync('server-key.pem'),
cert: fs.readFileSync('server-cert.pem')
};
tls.createServer(options, function (s) {
s.write("welcome!\n");
s.pipe(s);
}).listen(8000);