16

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"),
      http = require("http");

var privateKey = fs.readFileSync('/home/privatekey.pem');
var certificate = fs.readFileSync('/home/certificate.pem');

var credentials = crypto.createCredentials({key: privateKey.toString(), cert: certificate.toString()});

var handler = function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
};

var server = http.createServer();
server.setSecure(credentials);
server.addListener("request", handler);
server.listen(8084);

But when I start my server, I get the following error:

node.js:116
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
TypeError: Object #<Server> has no method 'setSecure'
    at Object.<anonymous> (/home/meshdev/public_html/js/node/server/test.js:16:8)
    at Module._compile (module.js:380:26)
    at Object..js (module.js:386:10)
    at Module.load (module.js:312:31)
    at Function._load (module.js:273:12)
    at Array.<anonymous> (module.js:399:10)
    at EventEmitter._tickCallback (node.js:108:26)

My server works great without the server.setSecure(credentials); line. I am running node.js(V0.4.1).

I would appreciate any suggestions.

Thank you.

3 Answers 3

20

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);
Sign up to request clarification or add additional context in comments.

7 Comments

@schaermu - Thanks for the reply. It has stopped my node server from returning errors on startup but I am now getting this error thrown up when I connect client side: pastebin.com/ECf1fxLB Any ideas?
could you post the content of s.authorizationError ? There should be a better error message when a tls handshake is failing (according the docs).
Sorry. My mistake. I am using this code for the server: pastebin.com/FHmVpPS9. And this code for the client side connection: pastebin.com/xxcL7Rqv. The node server no longer errors, but the client side cannot connect.
p.s. I am using socket.io to connect to the server.
oh, didn't see the socket.io tag ;) maybe you want to take a look at the ssl-example of socket.io. I never implemented a secure socket.io server/client, can't help you out with my own experience.
|
3

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);

Comments

1

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);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.