我正在尝试创建一个安全的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)。
server.setSecure(credentials);
我将不胜感激任何建议。
谢谢。
在Node.JS 0.4中重新执行了HTTPS实现。请参阅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);