Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
@brianleroux

This comment has been minimized.

Copy link

brianleroux commented Jan 4, 2011

beautiful!!

@dshaw

This comment has been minimized.

Copy link

dshaw commented Jan 4, 2011

Impressively simple as always.

@rgaidot

This comment has been minimized.

Copy link

rgaidot commented Jan 4, 2011

yes!

@aviflax

This comment has been minimized.

Copy link

aviflax commented Jan 4, 2011

love it!

@tanepiper

This comment has been minimized.

Copy link

tanepiper commented Jan 4, 2011

+2 for simplicity

@jonashuckestein

This comment has been minimized.

Copy link

jonashuckestein commented Jan 4, 2011

I love this :) Good job!

@ryanmcgrath

This comment has been minimized.

Copy link

ryanmcgrath commented Jan 4, 2011

Awesome, can't wait to use it!

@3rd-Eden

This comment has been minimized.

Copy link

3rd-Eden commented Jan 4, 2011

Nice.. But wouldn't it be easier if it was directly exposed in the http module instead of separate https? eg:

var http = require("http")
,       ssl  = { key: .. , cert: .. };

var https = http.createServer( ssl, function(){})
,       http = http.createServer( function(){} );
@yyliang

This comment has been minimized.

Copy link

yyliang commented Jan 7, 2011

what's the difference between module tls and https?

@pyrotechnick

This comment has been minimized.

Copy link

pyrotechnick commented Jan 9, 2011

@yyliang https is to tls as http is to net

@tj

This comment has been minimized.

Copy link

tj commented Jan 9, 2011

the options should be the second argument of createServer() IMO

@dshaw

This comment has been minimized.

Copy link

dshaw commented Jan 9, 2011

@visionmedia If options were optional, I'd agree. But, since key and cert are required config, making it the first parameter makes more sense because the requestListener function is more readable as the final parameter IMHO. Perhaps not calling the first parameter "options" would make that more clear.

@tj

This comment has been minimized.

Copy link

tj commented Jan 9, 2011

actually yeah i guess it depends how large your function is, I was just thinking it makes more sense for complying with the http server api, and then just tacking the options on, more elegant to support both that way

@dshaw

This comment has been minimized.

Copy link

dshaw commented Jan 9, 2011

Right on. Good point.

@pyrotechnick

This comment has been minimized.

Copy link

pyrotechnick commented Jan 10, 2011

visionmedia++

@sveisvei

This comment has been minimized.

Copy link

sveisvei commented Jan 10, 2011

Nice.

Not so sure about the options arg last. Yes, its more like the http-server api, but it just looks ugly - and feels unatural when reading and have to jump way down to read the second parameter.
Callbacks last is imho a more important "standard" to strive for.

Cheers:)

@pyrotechnick

This comment has been minimized.

Copy link

pyrotechnick commented Jan 10, 2011

"callbacks last"++ -- it really helps when using CoffeeScript

@tj

This comment has been minimized.

Copy link

tj commented Jan 10, 2011

it just looks ugly? do you realize which language you are using lol think of it this way:

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

options can be safely ignore when http, but support simple swapping for https

@creationix

This comment has been minimized.

Copy link

creationix commented Jan 10, 2011

Only slight longer, but very clear and simple:

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

This comment has been minimized.

Copy link

sveisvei commented Jan 10, 2011

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

This comment has been minimized.

Copy link

tj commented Jan 11, 2011

yeah that looks better

@tilgovi

This comment has been minimized.

Copy link

tilgovi commented Jan 13, 2011

(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

This comment has been minimized.

Copy link

tj commented Jan 13, 2011

dude that is nasty lol wtf

@rikkert

This comment has been minimized.

Copy link

rikkert commented Jan 13, 2011

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
You can’t perform that action at this time.