kurisukun
kurisukun

Reputation: 3159

Node.js https API server, but http for static files

I am making an API server with Node.js for an iPhone client. I am also using this node server to send back some requested files like static images and such.

Right now I am creating a https server with the following code:

https.createServer(options,app).listen(app.get('port'), function(){
    console.log("Express server listening on port " + app.get('port'));
});

which works fine.

However, I would like the static files to be served over HTTP and not HTTPS. Is this possible?

I am serving the static files with the code below:

app.use(express.static(path.join(__dirname, 'uploads')));

Thank you!

Upvotes: 1

Views: 2506

Answers (1)

Nick Mitchinson
Nick Mitchinson

Reputation: 5480

If you are looking to do this on the same port, this would pretty much be a duplicate of Binding HTTP and HTTPS traffic on the SAME port in node.js?.

However if you have multiple ports (one for HTTP and one for HTTPS) you could try just starting two instances of the server. Create two apps (which can be done in the same node file), one for your actual API and then a smaller one called, say, staticApp for serving your static content. Then your code for starting your API server is the same, and you can just use

http.createServer(app).listen(app.get('port'), function(){
    console.log("Static server listening on port " + app.get('port'));
});

To avoid confusion you could also change the name of the 'port' variable in your API server to be something like 'ssl-port' so you dont get confused.

This will effectively create two servers; one serving your static content over HTTP, and leave your original API servering serving over HTTPS. Just be sure that you are using either 80/443 (standard) so that the ports are impliclty interpreted by the protocol (ie, http://mydomain.com and https://mydomain.com go to the right places), or your load-balancer or whatever is proxying your traffic is sending the right protocols to the right ports.

Note that this would give a 404 (most likely) to anyone attempting to access your API over HTTP. A solution to that could be to add generic routes that match your API endpoints (something like api/*, if that's what your structures set up as) that redirect the user to the same path using https protocol.

In response to your comment

Two make two applications in one file, just do something like:

var api = express();

api.configure(function(){
    app.set('ssl_port', [your https port]);
    //your api code
});


var static = express();

static.configure(function () {
    app.set('port', [your http port]); // Set the port
    app.use(express.static(path.join(__dirname, 'static'))); //This will serve everything inside '/static' as static content
})

Too create the two servers:

//HTTPS Api Server
https.createServer(https_options, app).listen(app.get('ssl_port'), function(){
  console.log("Api server listening on port " + app.get('ssl_port'));
});

//HTTP Static Server
http.createServer(static).listen(static.get('port'), function () {
    console.log('Static server listening on %s', static.get('port'));
});

Upvotes: 4

Related Questions