xsnippet logotype

xsnippet

#363724, Untitled [ Text only ]

by Guest
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// init config

const config = require('../config');
const compress = require('compression');
const cors = require('cors');
const helmet = require('helmet');
const bodyParser = require('body-parser');
const logger = require('./logger');

const feathers = require('@feathersjs/feathers');
const express = require('@feathersjs/express');
const configuration = require('@feathersjs/configuration');
const rest = require('@feathersjs/express/rest');
const errorHandler = require('@feathersjs/errors/lib/error-handler');
const db = require('./models');
const notFound = require('feathers-errors/not-found');

const middleware = require('./middleware');
const services = require('./services');
const appHooks = require('./app.hooks');

const app = express(feathers());

db(app);

// Load app configuration
app.configure(configuration());
app.configure(rest());
// Enable CORS, security, compression, favicon and body parsing
app.use(cors({
	credentials: true,
	origin: (origin, callback) => {
		if (config.environment === 'development' || config.environment === 'test') {
			callback(null, true)
		} else if (config.cors.origins[config.environment].indexOf(origin) !== -1 || !origin) {
			callback(null, true)
		} else {
			callback(new Error(`Not allowed by CORS for origin ${origin}`))
		}
	},
	optionsSuccessStatus: 200
}));
app.use(helmet());
app.use(compress());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use((request, res, next) => {
  request.feathers.req = request;
  next();
});

app.set('logger', logger);
// Configure other middleware (see `middleware/index.js`)
app.configure(middleware);
// Set up our services (see `services/index.js`)
app.configure(services);
// Configure a middleware for 404s and the error handler
app.use(notFound());
app.use(
  errorHandler({
    json: {
      400: (error, request, response) => {
        response.status(error.code).json(error.errors);
      },
      404: (error, request, response) => {
        response.status(error.code).json();
      },
      500: (error, request, response) => {
        const message = error.message ? [error.message] : [];

        response.status(error.code).json(message);
      },
      default: (error, request, response) => {
        const responseError = {
          code: error.code || 500,
          errors: error.message ? [error.message] : []
        };

        response.status(responseError.code).json(responseError);
      }
    }
  })
);

app.hooks(appHooks);

module.exports = app;