Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTPS with nodejs and connect

I'm currently using nodejs with connect as my HTTP server. Is there anyway to activate HTTPS with connect? I cannot find any documentation about it. Thanks.

Herry

like image 205
Herry Avatar asked Mar 17 '13 04:03

Herry


People also ask

How do I enable HTTPS in Node JS?

To start your https server, run node app. js (here, app. js is name of the file) on the terminal. or in your browser, by going to https://localhost:8000 .

Does Node JS support HTTPS?

HTTPS is the HTTP protocol over TLS/SSL. In Node. js this is implemented as a separate module.

Can you create an HTTP web server with Node JS?

Node.js as a Web ServerThe HTTP module can create an HTTP server that listens to server ports and gives a response back to the client.

How do I create a node https server?

To built an HTTPS server with nodeJs, we need an SSL (Secure Sockets Layer) certificate. We can create a self-signed SSL certificate on our local machine. Let's first create an SSL certificate on our machine first. After running this command, we would get some options to fill.


2 Answers

Instead of creating http server, use https server for connect :

var fs = require('fs');
var connect = require('connect')
  //, http = require('http'); Use https server instead
  , https = require('https');

var options = {
    key:    fs.readFileSync('ssl/server.key'),
    cert:   fs.readFileSync('ssl/server.crt'),
    ca:     fs.readFileSync('ssl/ca.crt')
};
var app = connect();
https.createServer(options,app).listen(3000);

See the documentation for https here and tls server (https is a subclass of tls) here

like image 70
user568109 Avatar answered Nov 15 '22 15:11

user568109


From http://tjholowaychuk.com/post/18418627138/connect-2-0

HTTP and HTTPS

Previously connect.Server inherited from Node’s core net.Server, this made it difficult to provide both HTTP and HTTPS for your application. The result of connect() (formerly connect.createServer()) is now simply a JavaScript Function. This means that you may omit the call to app.listen(), and simply pass app to a Node net.Server as shown here:

var connect = require('connect')
  , http = require('http')
  , https = require('https');

var app = connect()
  .use(connect.logger('dev'))
  .use(connect.static('public'))
  .use(function(req, res){
    res.end('hello world\n');
  })

http.createServer(app).listen(80);
https.createServer(tlsOptions, app).listen(443);

The same is true for express 3.0 since it inherits connect 2.0

like image 44
generalhenry Avatar answered Nov 15 '22 16:11

generalhenry