Skip to content

Instantly share code, notes, and snippets.

@ry
Created January 4, 2011 00:03
Show Gist options
  • Select an option

  • Save ry/764213 to your computer and use it in GitHub Desktop.

Select an option

Save ry/764213 to your computer and use it in GitHub Desktop.
new node https api
// curl -k https://localhost:8000/
var https = require('https');
var fs = require('fs');
var options = {
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
@creationix

Copy link
Copy Markdown

Only slight longer, but very clear and simple:

(useHTTPS ? http.createServer(callback) : https.createServer(options, callback))

@sveisvei

Copy link
Copy Markdown

In my puny head, it makes my eyes jump around more than the first example - I, imho, like when I can see input first, and then apply those to what I see inside the "output". Altough I see your point(both), for me at least input-output trumphs those now. Convince me otherwise :).

https.createServer(function(req, res){
  req.parseSomething(function(){
    req.checkSomething();
    // ....etc
    // ....etc
    // ....etc
    // ....etc
    // ....alot of code, and options down in the bottom.
  });
}, options);

@tj

tj commented Jan 11, 2011

Copy link
Copy Markdown

yeah that looks better

@tilgovi

tilgovi commented Jan 13, 2011

Copy link
Copy Markdown
(useHTTPS ? function (cb) { https.createServer(options, cb) } : http.createServer)(function (req, res) {
  res.writeHead(200);
  res.end("Don't be silly. Callbacks are always last.")
}).listen(8080);

@tj

tj commented Jan 13, 2011

Copy link
Copy Markdown

dude that is nasty lol wtf

@rikkert

rikkert commented Jan 13, 2011

Copy link
Copy Markdown

It is very sweet the way it is, just like we are used with require('http').
Great stuff tnx!

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