Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Track response times
app.use(responseTime());
//
// Set up Sentry logging
//
if (process.env.SENTRY_DSN) {
raven.patchGlobal(function(logged, err) {
console.log('Uncaught error. Reporting to Sentry and exiting.');
console.error(err.stack);
process.exit(1);
});
app.use(raven.middleware.express());
}
if (process.env.NODE_ENV !== 'production') {
app.use(morgan('dev'));
}
// you'll need cookies
app.use(cookieParser());
app.use(cors());
//
// Internationalization time (i18n)
//
i18n.configure({
locales: ['en', 'es'],
pass: (parsed_redis_connection_url.auth || '').split(':')[1]
}),
resave: false,
saveUninitialized: false
}) );
// Compress responses
server.use( express.compress() );
// Static file serving
server.use( express.static( __dirname + '/assets' ) );
// Exception tracking
if( SENTRY_DSN ){
var raven = require('raven');
server.use( raven.middleware.express( SENTRY_DSN ) );
}
//
// Passport configuration
//
var passport = require('passport');
var guestSetup = require('../middleware/guest.js');
server.use( passport.initialize() );
server.use( passport.session() );
server.use( guestSetup );
// Routing comes last
var morgan = require('morgan'); // Normal logs
var app = express();
app.disable('x-powered-by');
app.set('port', process.env.PORT || 1339);
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
if(process.env.NODE_ENV === 'production'){
console.log('✓ Loaded Sentry Log');
app.use(raven.middleware.express(process.env.SENTRY_URL));
}
app.use(morgan('dev'));
require(path.join(__dirname, '/routes/series'))(app);
require(path.join(__dirname, '/routes/list'))(app);
require(path.join(__dirname, '/routes/user'))(app);
app.use(function(err, req, res, next){
if(err){
// Do error handling
console.log(err.stack);
res.status(500).json({ message: err.message, status: "error" });
}
next();
});
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
// Sentry (Raven) middleware
app.use(raven.middleware.express.requestHandler(config.DSN));
// Should come before any other error middleware
app.use(raven.middleware.express.errorHandler(config.DSN));
// Assume 'not found' in the error msgs is a 404. this is somewhat silly, but valid, you can do whatever you like, set properties, use instanceof etc.
app.use(function(err, req, res, next) {
// If the error object doesn't exists
if (!err) {
return next();
}
// Log it
client.captureError(err);
// Error page
res.status(500).render('500', {
error: err.stack
});
});
badPath: 'This is not a valid path for this service. Please refer to the documentation: https://github.com/mozilla/page-metadata-service#url-metadata',
headerRequired: 'The content-type header must be set to application/json.',
urlsRequired: 'The post body must be a JSON payload in the following format: {urls: ["http://example.com"]}.',
maxUrls: 'A maximum of 20 urls can be sent for processing in one call.'
};
const app = express();
// Maximum number of URLs per request
app.set('maxUrls', 20);
const sentryDSN = process.env.SENTRY_DSN;
const sentryClient = new raven.Client(sentryDSN);
// The request handler must be the first item
app.use(raven.middleware.express.requestHandler(sentryDSN));
// For parsing application/json
app.use(bodyParser.json());
// Disable x-powered-by header
app.disable('x-powered-by');
app.post('/v1/metadata', function(req, res) {
const responseData = {
request_error: '',
url_errors: {},
urls: {}
};
const fail = (reason, status) => {
responseData.request_error = reason;
context,
graphiql: true,
})
graphqlMiddleware(req, res, next)
})
app.get('/github-login', routes.githubLogin)
app.get('/oauth-callback', routes.githubOAuthCallback)
app.get('/:user/:snippet/result', routes.snippetResult)
app.get('/:user/:snippet/download', routes.snippetDownload)
app.get('/oembed', routes.snippetEmbed)
SENTRY_URL && app.use(raven.middleware.express.errorHandler(SENTRY_URL))
if (SENTRY_URL) {
(new raven.Client(SENTRY_URL)).patchGlobal()
}
if (process.env.NUKE_DATA === '1') {
db.importDb()
}
const PORT = 3000
app.listen(PORT, () => console.log('Server listening on port ' + PORT))
}
return res.json({
message: 'System OK',
});
});
app.use('/api/v1/', require('./routes/api_v1'));
app.use((req, res, next) => {
res.status(404).json({
message: 'Not Found',
});
});
app.use(raven.middleware.express.requestHandler(sentry));
app.use(raven.middleware.express.errorHandler(sentry));
app.use((err, req, res, next) => {
if (err.code === 'LIMIT_UNEXPECTED_FILE') {
err.message = `Unknown form field "${err.field}"`;
err.code = 400;
}
err.code = err.code || 500;
res.status(err.code);
if (err.code >= 500) {
console.error(err);
console.error(err.message);
console.error(err.stack);
}
app.use(require('./routes/api.js'));
app.use(require('./routes/meta.js'));
app.use('/test', require('./routes/test.js'));
if (process.env.RUM_MYSQL_DSN) {
app.use(require('./routes/rum.js'));
}
app.get(/^(?:\/(?:docs\/?(?:(.+)\/?)?)?)?$/, require('./routes/docs'));
app.get(/^\/(?:v([12])(?:\/(?:docs\/?(?:(.+)\/?)?)?)?)?$/, require('./routes/docs'));
app.use(/^\/v[12]\/assets/, express.static(__dirname + '/../docs/assets'));
if (process.env.SENTRY_DSN) {
app.use(Raven.middleware.express.errorHandler(ravenClient));
}
function startService(port, callback) {
callback = callback || function() {};
app.server = app
.listen(port, function (err) {
callback(err, app);
})
.on('error', function (err) {
callback(err);
})
.on('clientError', function (ex, sock) {
sock.end('HTTP/1.1 400 Bad Request\r\n\r\n');
sock.destroy();
app.use(raven.middleware.express.requestHandler(log.ravenClient));
app.use(morgan('combined'));
app.use(getBasePath);
app.use(logHostname);
app.origami = {
log: options.log,
options
};
mountRoutes(app);
app.use(errorMiddleware);
app.use(raven.middleware.express.errorHandler(log.ravenClient));
return app;
};
function buildService(options) {
options.registry = new Registry(options);
const app = express();
app.disable('x-powered-by');
app.use(require('cors')());
app.use(raven.middleware.express.requestHandler(log.ravenClient));
app.use(morgan('combined'));
app.use(getBasePath);
app.use(logHostname);
app.origami = {
log: options.log,
options
};
mountRoutes(app);
app.use(errorMiddleware);
app.use(raven.middleware.express.errorHandler(log.ravenClient));