Skip to content

Instantly share code, notes, and snippets.

@eldyvoon
Created December 1, 2016 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eldyvoon/7a1df560fd9d13da74d090e28f7ee801 to your computer and use it in GitHub Desktop.
Save eldyvoon/7a1df560fd9d13da74d090e28f7ee801 to your computer and use it in GitHub Desktop.
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var fs = require('fs');
var https = require('https');
var https_port = '3002';
var app = express();
app.all('*', function(req, res, next){
if (req.secure) {
return next();
};
res.redirect('https://'+req.hostname+':'+https_port+req.url);
});
app.set('port', 3001);
app.use(sass({ src: path.join(__dirname, 'public'), dest: path.join(__dirname, 'public') }));
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({ extended: false },{ limit: '50mb' }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// for angularjs route
app.get('*', function(req, res) {
res.redirect('/#' + req.originalUrl);
});
// Production error handler
if (app.get('env') === 'production') {
app.use(function(err, req, res, next) {
console.error(err.stack);
res.sendStatus(err.status || 500);
});
}
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
var ca, cert, chain, line;
ca = [];
chain = fs.readFileSync('./ssl/mywebsite_my.ca-bundle', 'utf8');
chain = chain.split("\n");
cert = [];
for (var i = 0; i < chain.length; i++) {
line = chain[i];
if (!(line.length !== 0)) {
continue;
}
cert.push(line);
if (line.match(/-END CERTIFICATE-/)) {
ca.push(cert.join("\n"));
cert = [];
}
}
var secureServer = https.createServer({
ca: ca,
key: fs.readFileSync('./ssl/mywebsite.key', 'utf8'),
cert: fs.readFileSync('./ssl/mywebsite_my.crt', 'utf8')
}, app)
.listen(https_port, function () {
console.log('Secure Server listening on port ' + https_port);
});
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment