Hey Alex. Already some good comments here but it's generally not a problem to serve some routes over HTTP and others over HTTPS per se. However mixing http/https on the same page is the so-called "mixed content" vulnerability and most browsers will complain about it. If you just want to serve your About and Search sections over HTTP, but other things over HTTPS, that should be fine. HTTPS and HTTP run on different ports (typically 443 and 80, respectively) so in any case, you need to have express server listening for HTTP, and another serving HTTPS. I wouldn't recommend trying instantiate both servers in the same app. Instead, you can divide your routes into separate files under the routes/ directory. The app that serves HTTP can include only the About and Search route modules, and the app that serves HTTPS can include those, plus the Account routes (so they're all available over HTTPS).
You'll probably want to include a redirect in the HTTP server when a user tries to access one of the secure routes. If for whatever reason you want to serve both HTTP and HTTPS from the same process, it's possible to create two express apps in the same module (ie. app.js file) but I wouldn't recommend it for performance and reliability reasons (ie. it would be nice to continue serving HTTP if you take HTTPS offline for whatever reason).
As others mentioned, using something like nginx in a production deployment is pretty common and has advantages. It's worth looking into, but not the first solution I'd suggest for starting development.
For a good example of route separation, see
https://github.com/visionmedia/express/tree/master/examples/route-separation Another pattern, which doesn't seem to be in the examples for some reason, is to structure your route modules to accept the express server instance as an argument, to which they can append routes:
// module exports function that accepts an 'app' parameter
module.exports = function (app) {
// routes are attached to the app as usual
app.get("/dashboard", function (req, res) {
if (req.session.user) {
res.render('dashboard.ejs', {'title':'Dashboard'});
} else {
res.redirect('/login');
}
});
};
The app that uses the routes passes itself to the route module to have those routes added to it:
// main app.js example
// require route modules and pass in the app
var app = express();
require('./routes/dashboard.js')(app);
require('./routes/portal.js')(app);
This has been a really handy way to modularize routes; you might find it useful for organizing your routes for inclusion in separate http and https express servers, too.
Serving everything over HTTPS is not a bad recommendation. For me, it's been easier than worrying about two servers. However, in this case you probably want to include a redirect from HTTP to HTTPS. This is easy to do in your main app by simply instantiating another express server, making it listen on the HTTP port, and responding to everything with a redirect to the HTTPS port. You can even get fancy and grab the request path, and append it to the redirect path so that URLs will forward from http to https and keep their full path.
var redirector = express();
redirector.all("*", function (req, res) {
res.redirect("https://" + req.host + ":3000" + req.path);
});Last thing, express apps are often run as a non-privileged user with HTTP on port 8080 and HTTPS on port 3000 (running as root on port 80 and/or 443 is not recommended for security reasons). To transparently proxy clients from the default HTTP and HTTPS ports to these non-privileged ports, once again you can use nginx, or simply set up forwarding rules using iptables (in linux). See
http://proghowto.com/iptables-redirect-port-80-to-port-8080 for an example.
hope this helps,
Darren