2014-12-22 16:16:10 -08:00
|
|
|
require('dotenv').load();
|
2015-03-22 22:18:01 -07:00
|
|
|
// handle uncaught exceptions. Forever will restart process on shutdown
|
|
|
|
process.on('uncaughtException', function (err) {
|
|
|
|
console.error(
|
|
|
|
(new Date()).toUTCString() + ' uncaughtException:',
|
|
|
|
err.message
|
|
|
|
);
|
|
|
|
console.error(err.stack);
|
2015-06-01 16:23:53 -07:00
|
|
|
process.exit(1); // eslint-disable-line
|
2015-03-22 22:18:01 -07:00
|
|
|
});
|
2014-01-11 22:53:31 -05:00
|
|
|
|
2015-06-02 17:27:02 -07:00
|
|
|
var loopback = require('loopback'),
|
|
|
|
boot = require('loopback-boot'),
|
2015-04-14 15:36:02 -04:00
|
|
|
accepts = require('accepts'),
|
|
|
|
cookieParser = require('cookie-parser'),
|
|
|
|
compress = require('compression'),
|
|
|
|
session = require('express-session'),
|
|
|
|
logger = require('morgan'),
|
|
|
|
errorHandler = require('errorhandler'),
|
|
|
|
methodOverride = require('method-override'),
|
|
|
|
bodyParser = require('body-parser'),
|
|
|
|
helmet = require('helmet'),
|
|
|
|
MongoStore = require('connect-mongo')(session),
|
|
|
|
flash = require('express-flash'),
|
|
|
|
path = require('path'),
|
|
|
|
passport = require('passport'),
|
|
|
|
expressValidator = require('express-validator'),
|
2015-06-01 16:23:53 -07:00
|
|
|
// request = require('request'),
|
2015-05-01 16:34:07 -04:00
|
|
|
forceDomain = require('forcedomain'),
|
2015-05-03 16:45:34 -07:00
|
|
|
lessMiddleware = require('less-middleware'),
|
2014-01-11 22:53:31 -05:00
|
|
|
|
2015-05-19 22:31:01 -04:00
|
|
|
/**
|
|
|
|
* API keys and Passport configuration.
|
|
|
|
*/
|
2015-06-01 17:01:54 -07:00
|
|
|
secrets = require('./../config/secrets');
|
2013-11-26 23:15:13 -05:00
|
|
|
|
2014-02-02 05:38:38 -05:00
|
|
|
/**
|
|
|
|
* Create Express server.
|
|
|
|
*/
|
2015-06-02 17:27:02 -07:00
|
|
|
var app = loopback();
|
2015-02-17 15:35:16 -08:00
|
|
|
|
2014-01-11 22:53:31 -05:00
|
|
|
app.set('port', process.env.PORT || 3000);
|
|
|
|
app.set('views', path.join(__dirname, 'views'));
|
|
|
|
app.set('view engine', 'jade');
|
2015-04-22 14:53:58 -07:00
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
2015-05-01 16:34:07 -04:00
|
|
|
app.use(forceDomain({
|
|
|
|
hostname: 'www.freecodecamp.com'
|
|
|
|
}));
|
2015-04-22 14:53:58 -07:00
|
|
|
}
|
|
|
|
|
2014-06-06 14:58:30 -04:00
|
|
|
app.use(compress());
|
2015-06-01 16:23:53 -07:00
|
|
|
app.use(lessMiddleware(path.join(__dirname, '/public')));
|
2014-04-12 12:43:07 -04:00
|
|
|
app.use(logger('dev'));
|
|
|
|
app.use(bodyParser.json());
|
2015-06-01 16:23:53 -07:00
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
2014-12-23 18:20:53 -08:00
|
|
|
app.use(expressValidator({
|
2015-05-25 19:55:18 -04:00
|
|
|
customValidators: {
|
|
|
|
matchRegex: function (param, regex) {
|
|
|
|
return regex.test(param);
|
2014-12-23 18:20:53 -08:00
|
|
|
}
|
2015-05-25 19:55:18 -04:00
|
|
|
}
|
2014-12-23 18:20:53 -08:00
|
|
|
}));
|
2014-04-12 12:43:07 -04:00
|
|
|
app.use(methodOverride());
|
|
|
|
app.use(cookieParser());
|
|
|
|
app.use(session({
|
2015-04-14 15:36:02 -04:00
|
|
|
resave: true,
|
|
|
|
saveUninitialized: true,
|
|
|
|
secret: secrets.sessionSecret,
|
|
|
|
store: new MongoStore({
|
|
|
|
url: secrets.db,
|
2015-04-16 23:16:55 -07:00
|
|
|
'autoReconnect': true
|
2015-04-14 15:36:02 -04:00
|
|
|
})
|
2014-01-29 00:49:09 -05:00
|
|
|
}));
|
2014-01-11 22:53:31 -05:00
|
|
|
app.use(passport.initialize());
|
|
|
|
app.use(passport.session());
|
2014-06-01 11:52:28 -04:00
|
|
|
app.use(flash());
|
2014-12-10 20:44:33 -08:00
|
|
|
app.disable('x-powered-by');
|
2014-12-23 08:48:28 -08:00
|
|
|
|
2014-12-10 20:44:33 -08:00
|
|
|
app.use(helmet.xssFilter());
|
2015-01-09 15:10:34 -08:00
|
|
|
app.use(helmet.noSniff());
|
2015-05-06 19:10:03 -04:00
|
|
|
app.use(helmet.frameguard());
|
2015-02-17 15:35:16 -08:00
|
|
|
app.use(function(req, res, next) {
|
2015-05-25 19:55:18 -04:00
|
|
|
res.header('Access-Control-Allow-Origin', '*');
|
|
|
|
res.header('Access-Control-Allow-Headers',
|
|
|
|
'Origin, X-Requested-With, Content-Type, Accept'
|
|
|
|
);
|
|
|
|
next();
|
2015-02-17 15:35:16 -08:00
|
|
|
});
|
2014-12-23 08:48:28 -08:00
|
|
|
|
2014-12-22 12:36:45 -08:00
|
|
|
var trusted = [
|
2015-04-14 15:36:02 -04:00
|
|
|
"'self'",
|
2015-05-06 18:14:00 -04:00
|
|
|
'blob:',
|
2015-04-14 15:36:02 -04:00
|
|
|
'*.freecodecamp.com',
|
2015-05-06 19:10:03 -04:00
|
|
|
'http://www.freecodecamp.com',
|
|
|
|
'ws://freecodecamp.com/',
|
|
|
|
'ws://www.freecodecamp.com/',
|
2015-04-14 15:36:02 -04:00
|
|
|
'*.gstatic.com',
|
|
|
|
'*.google-analytics.com',
|
|
|
|
'*.googleapis.com',
|
|
|
|
'*.google.com',
|
|
|
|
'*.gstatic.com',
|
|
|
|
'*.doubleclick.net',
|
|
|
|
'*.twitter.com',
|
|
|
|
'*.twitch.tv',
|
|
|
|
'*.twimg.com',
|
|
|
|
"'unsafe-eval'",
|
|
|
|
"'unsafe-inline'",
|
|
|
|
'*.bootstrapcdn.com',
|
|
|
|
'*.cloudflare.com',
|
|
|
|
'https://*.cloudflare.com',
|
|
|
|
'localhost:3001',
|
|
|
|
'ws://localhost:3001/',
|
|
|
|
'http://localhost:3001',
|
|
|
|
'localhost:3000',
|
|
|
|
'ws://localhost:3000/',
|
|
|
|
'http://localhost:3000',
|
|
|
|
'*.ionicframework.com',
|
|
|
|
'https://syndication.twitter.com',
|
|
|
|
'*.youtube.com',
|
|
|
|
'*.jsdelivr.net',
|
|
|
|
'https://*.jsdelivr.net',
|
|
|
|
'*.ytimg.com',
|
2015-04-14 23:04:54 -04:00
|
|
|
'*.bitly.com',
|
|
|
|
'http://cdn.inspectlet.com/',
|
2015-05-28 16:40:22 -07:00
|
|
|
'wss://inspectletws.herokuapp.com/',
|
|
|
|
'http://hn.inspectlet.com/'
|
2014-12-22 12:36:45 -08:00
|
|
|
];
|
2014-12-22 13:38:48 -08:00
|
|
|
|
2015-05-06 19:10:03 -04:00
|
|
|
app.use(helmet.csp({
|
2015-05-25 19:55:18 -04:00
|
|
|
defaultSrc: trusted,
|
|
|
|
scriptSrc: [
|
|
|
|
'*.optimizely.com',
|
|
|
|
'*.aspnetcdn.com',
|
|
|
|
'*.d3js.org'
|
|
|
|
].concat(trusted),
|
|
|
|
'connect-src': [
|
|
|
|
].concat(trusted),
|
|
|
|
styleSrc: trusted,
|
|
|
|
imgSrc: [
|
|
|
|
/* allow all input since we have user submitted images for public profile*/
|
|
|
|
'*'
|
|
|
|
].concat(trusted),
|
|
|
|
fontSrc: ['*.googleapis.com'].concat(trusted),
|
|
|
|
mediaSrc: [
|
|
|
|
'*.amazonaws.com',
|
|
|
|
'*.twitter.com'
|
|
|
|
].concat(trusted),
|
|
|
|
frameSrc: [
|
|
|
|
|
|
|
|
'*.gitter.im',
|
|
|
|
'*.gitter.im https:',
|
|
|
|
'*.vimeo.com',
|
|
|
|
'*.twitter.com',
|
|
|
|
'*.ghbtns.com'
|
|
|
|
].concat(trusted),
|
2015-06-01 16:23:53 -07:00
|
|
|
// set to true if you only want to report errors
|
|
|
|
reportOnly: false,
|
|
|
|
// set to true if you want to set all headers
|
|
|
|
setAllHeaders: false,
|
|
|
|
// set to true if you want to force buggy CSP in Safari 5
|
|
|
|
safari5: false
|
2014-12-10 20:44:33 -08:00
|
|
|
}));
|
2014-11-19 15:30:36 -08:00
|
|
|
|
2015-06-02 17:27:02 -07:00
|
|
|
|
2015-01-16 18:58:27 -05:00
|
|
|
app.use(function (req, res, next) {
|
2015-04-14 15:36:02 -04:00
|
|
|
// Make user object available in templates.
|
|
|
|
res.locals.user = req.user;
|
|
|
|
next();
|
2014-01-11 22:53:31 -05:00
|
|
|
});
|
2014-11-19 15:30:36 -08:00
|
|
|
|
2015-06-01 16:23:53 -07:00
|
|
|
app.use(
|
2015-06-02 17:27:02 -07:00
|
|
|
loopback.static(path.join(__dirname, '../public'), { maxAge: 86400000 })
|
2015-06-01 16:23:53 -07:00
|
|
|
);
|
2015-05-06 09:10:15 -04:00
|
|
|
|
2015-06-02 17:27:02 -07:00
|
|
|
boot(app, __dirname);
|
2015-01-16 18:58:27 -05:00
|
|
|
app.use(function (req, res, next) {
|
2015-05-25 19:55:18 -04:00
|
|
|
// Remember original destination before login.
|
|
|
|
var path = req.path.split('/')[1];
|
|
|
|
if (/auth|login|logout|signin|signup|fonts|favicon/i.test(path)) {
|
|
|
|
return next();
|
|
|
|
} else if (/\/stories\/comments\/\w+/i.test(req.path)) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
req.session.returnTo = req.path;
|
|
|
|
next();
|
2014-03-08 14:58:27 -05:00
|
|
|
});
|
2014-11-19 15:30:36 -08:00
|
|
|
|
2014-02-01 03:30:14 -05:00
|
|
|
/**
|
2014-06-06 14:58:30 -04:00
|
|
|
* OAuth sign-in routes.
|
2014-02-01 03:30:14 -05:00
|
|
|
*/
|
2014-12-23 13:50:14 -08:00
|
|
|
|
2014-11-19 15:30:36 -08:00
|
|
|
/**
|
|
|
|
* 500 Error Handler.
|
|
|
|
*/
|
2015-03-24 08:03:59 -07:00
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
app.use(errorHandler({ log: true }));
|
|
|
|
} else {
|
2015-06-01 16:23:53 -07:00
|
|
|
// error handling in production disabling eslint due to express parity rules
|
|
|
|
// for error handlers
|
|
|
|
app.use(function(err, req, res, next) { // eslint-disable-line
|
2015-03-24 08:03:59 -07:00
|
|
|
|
|
|
|
// respect err.status
|
|
|
|
if (err.status) {
|
|
|
|
res.statusCode = err.status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// default status code to 500
|
|
|
|
if (res.statusCode < 400) {
|
|
|
|
res.statusCode = 500;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse res type
|
|
|
|
var accept = accepts(req);
|
|
|
|
var type = accept.type('html', 'json', 'text');
|
|
|
|
|
|
|
|
var message = 'opps! Something went wrong. Please try again later';
|
|
|
|
if (type === 'html') {
|
|
|
|
req.flash('errors', { msg: message });
|
|
|
|
return res.redirect('/');
|
2015-04-14 15:36:02 -04:00
|
|
|
// json
|
2015-03-24 08:03:59 -07:00
|
|
|
} else if (type === 'json') {
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
return res.send({ message: message });
|
2015-04-14 15:36:02 -04:00
|
|
|
// plain text
|
2015-03-24 08:03:59 -07:00
|
|
|
} else {
|
|
|
|
res.setHeader('Content-Type', 'text/plain');
|
|
|
|
return res.send(message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-11-19 15:30:36 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Start Express server.
|
|
|
|
*/
|
2015-04-22 14:53:58 -07:00
|
|
|
|
2015-06-02 17:27:02 -07:00
|
|
|
app.start = function() {
|
|
|
|
app.listen(app.get('port'), function () {
|
|
|
|
console.log(
|
|
|
|
'FreeCodeCamp server listening on port %d in %s mode',
|
|
|
|
app.get('port'),
|
|
|
|
app.get('env')
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// start the server if `$ node server.js`
|
|
|
|
if (require.main === module) {
|
|
|
|
app.start();
|
|
|
|
}
|
2014-11-19 15:30:36 -08:00
|
|
|
|
|
|
|
module.exports = app;
|