Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const configureSentryRequestHandler = (app) => {
if (env !== 'development' && config.sentry.dsn) {
Sentry.init({ dsn: config.sentry.dsn });
// Sentry.Handlers.requestHandler should be the first middleware
app.use(Sentry.Handlers.requestHandler());
}
};
const appData = {
sentryDsn: process.env.SENTRY_PUBLIC_DSN,
sentryEnv: process.env.SENTRY_ENVIRONMENT,
};
/* configure Sentry */
Sentry.init({
dsn: process.env.SENTRY_DSN,
environment: process.env.SENTRY_ENVIRONMENT,
});
/* configure the express server */
const server = express();
// the first middleware must be the sentry request handler
server.use(Sentry.Handlers.requestHandler());
// set CORS and JSON middleware
server.use(cors());
server.use(express.json());
// serve public files
const statics = express.static(path.resolve(__dirname, 'public'));
server.use(statics);
// controllers
server.use('/example', exampleController);
// then fallback
server.use(history());
const renderHtml = () => {
var limiter = require('express-limiter')(app, redis);
// rate limit POST requests
limiter({
path: '*',
method: 'post',
lookup: ['headers.CF-Connecting-IP'],
// 10 requests per hour
total: 10,
expire: 1000 * 60 * 60
})
}
app.set('trust proxy', true);
// The Sentry request handler must be the first middleware on the app
app.use(Sentry.Handlers.requestHandler());
app.use(compress());
app.use(cookieParser());
app.use(helmet());
app.enable('etag');
app.use(checkForRedirects);
// check if this is a valid static file
app.use(express.static('dist', { etag: true }));
// cache images and static assets for 1 week
app.use(express.static('content', { maxAge: 1000 * 60 * 60 * 24 * 7 }));
app.use(prismicMiddleware);
app.use(loadLocalVars);
announcementBarCronJob(app)
private _initializeApp() {
// TODO: Maybe pass this in as a parameter instead of creating it here?
this.state.app = express()
if (this.options.useSentry) {
this._initSentry()
}
if (this.options.enableMetrics) {
this._initMetrics()
}
this.state.app.use(cors())
this._registerAllRoutes()
// Sentry error handling must be after all controllers
// and before other error middleware
if (this.options.useSentry) {
this.state.app.use(Sentry.Handlers.errorHandler())
}
}
const app = express();
const APP_PORT =
(process.env.NODE_ENV === 'test' ? process.env.TEST_APP_PORT : process.env.APP_PORT) || process.env.PORT || '3000';
const APP_HOST = process.env.APP_HOST || '0.0.0.0';
const pathToSwaggerUi = require('swagger-ui-dist').absolutePath();
app.set('port', APP_PORT);
app.set('host', APP_HOST);
app.locals.title = process.env.APP_NAME;
app.locals.version = process.env.APP_VERSION;
// This request handler must be the first middleware on the app
app.use(Sentry.Handlers.requestHandler());
app.use(favicon(path.join(__dirname, '/../public', 'favicon.ico')));
app.use(cors());
app.use(helmet());
app.use(compression());
app.use(morgan('tiny', { stream: logStream }));
app.use(bodyParser.json());
app.use(errorHandler.bodyParser);
app.use(json);
// API Routes
app.use('/api', routes);
// Swagger UI
// Workaround for changing the default URL in swagger.json
// https://github.com/swagger-api/swagger-ui/issues/4624
middlewares() {
// Sentry
this.server.use(Sentry.Handlers.requestHandler());
this.server.use(cors());
this.server.use(express.json());
this.server.use(
'/files',
express.static(path.resolve(__dirname, '..', 'tmp', 'uploads'))
);
}
scope.addEventProcessor((event) => Sentry.Handlers.parseRequest(event, ctx.request));
Sentry.captureException(err);
*/
app.use(stderrStream, stdoutStream);
app.use(bodyParser.graphql());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
/**
* ERROR HANDLING
*/
/**
* SENTRY: The error handler must be before any other error middleware
*/
app.use(Sentry.Handlers.errorHandler());
/**
* Catch 404 and forward to error handler
*/
app.use(notFoundErrorHandler);
/**
* The 'unhandledRejection' event is emitted whenever a Promise is rejected and
* no error handler is attached to the promise.
*/
process.on('unhandledRejection', unhandledRejectionHandler);
/**
* The 'uncaughtException' event is emitted when an uncaught JavaScript exception
* bubbles all the way back to the event loop omitting Express.js error handler.
*
exports.requestHandler = () => {
if (SENTRY_DSN) {
return Sentry.Handlers.requestHandler();
} else {
return (req, res, next) => {
next();
};
}
};