node.js with http and https routing -
currently make node.js project create both http , https server.
var httpserver = http.createserver(app); httpserver.listen(3000); var httpsoptions = { ca: fs.readfilesync(config.https.ssl.ca, 'utf8'), key: fs.readfilesync(config.https.ssl.key, 'utf8'), cert: fs.readfilesync(config.https.ssl.cert, 'utf8') }; var httpsserver = https.createserver(httpsoptions, app); httpsserver.listen(8000);
also used middleware redirect http traffic https.
app.all('*', function(req, res, next){ var host = req.header("host"); if (host && host.indexof('localhost') !== -1) { next() } else if (req.connection.encrypted) { next(); } else { res.redirect('https://' + host + req.url); } });
but pages not need https connections, http://www.domain.com/shops/ route. can make route use http method , other routes use https still?
p.s: page request resources other routes bower_components, public, ... etc.
you can find request path follows, might redirect request accordingly.
var url_parts = url.parse(req.url); console.log(url_parts); console.log(url_parts.pathname);
Comments
Post a Comment