Skip to content

Instantly share code, notes, and snippets.

@jeromeetienne
Created August 6, 2010 09:44
Show Gist options
  • Save jeromeetienne/511109 to your computer and use it in GitHub Desktop.
Save jeromeetienne/511109 to your computer and use it in GitHub Desktop.
example of node.js ssl server
// You can generate the privatekey.pem and certificate.pem files using the following commands:
//
// openssl genrsa -out privatekey.pem 1024
// openssl req -new -key privatekey.pem -out certrequest.csr
// openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem
const crypto = require('crypto'),
fs = require("fs"),
http = require("http");
var privateKey = fs.readFileSync('privatekey.pem');
var certificate = fs.readFileSync('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(8000);
@Draphar
Copy link

Draphar commented Apr 23, 2018

Warning, setSecure() and some other parts are not supported in node versions >0.4.

@hk20181001
Copy link

[root@iZj6c3z41dx386h1575vk2Z https]# node server.js
/root/https/server.js:16
server.setSecure(credentials);
^

TypeError: server.setSecure is not a function
at Object. (/root/https/server.js:16:8)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment