1 const createError
= require('http-errors');
2 const express
= require('express');
3 const path
= require('path');
4 const cookieParser
= require('cookie-parser');
5 const logger
= require('morgan');
6 const favicon
= require('serve-favicon');
7 const params
= require('./config/parameters');
11 app
.use(favicon(path
.join(__dirname
, "static", "favicon.ico")));
13 if (app
.get('env') === 'development')
15 // Full logging in development mode
16 app
.use(logger('dev'));
20 // http://dev.rdybarra.com/2016/06/23/Production-Logging-With-Morgan-In-Express/
21 app
.set('trust proxy', true);
22 // In prod, only log error responses (https://github.com/expressjs/morgan)
23 app
.use(logger('combined', {
24 skip: function (req
, res
) { return res
.statusCode
< 400 }
28 app
.use(express
.json());
29 app
.use(express
.urlencoded({ extended: false }));
30 app
.use(cookieParser());
31 app
.use(express
.static(path
.join(__dirname
, 'static'))); //client "prod" files
33 // In development stage the client side has its own server
34 if (params
.cors
.enable
)
36 app
.use(function(req
, res
, next
) {
37 res
.header("Access-Control-Allow-Origin", params
.cors
.allowedOrigin
);
38 res
.header("Access-Control-Allow-Credentials", true); //for cookies
39 res
.header("Access-Control-Allow-Headers",
40 "Origin, X-Requested-With, Content-Type, Accept");
41 res
.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE");
46 // Routing (AJAX-only)
47 const routes
= require(path
.join(__dirname
, "routes", "all"));
50 // catch 404 and forward to error handler
51 app
.use(function(req
, res
, next
) {
52 next(createError(404));
56 app
.use(function(err
, req
, res
, next
) {
57 // set locals, only providing error in development
58 res
.locals
.message
= err
.message
;
59 res
.locals
.error
= (req
.app
.get('env') === 'development' ? err : {});
60 // render the error page
61 res
.status(err
.status
|| 500);
65 <h2>= error.status</h2>
66 <pre>#{error.stack}</pre>