Node.js HTTPS安全错误

时间:2011-02-27 22:08:35

标签: https node.js socket.io

我正在尝试创建一个安全的node.js服务器,以便与使用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);

但是当我启动服务器时,出现以下错误:

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)

我的服务器在没有server.setSecure(credentials);行的情况下运行良好。我正在运行node.js(V0.4.1)。

我很感激任何建议。

谢谢。

3 个答案:

答案 0 :(得分:18)

HTTPS实现在Node.JS 0.4中重新完成。请参阅nodejs.org上的相应文档。

文档示例:

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

答案 1 :(得分:1)

此设置允许我连接到我的socket.io服务器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);

答案 2 :(得分:1)

我已经使用ssl处理https安全了这里是制作https和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);
});