This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
generated from stacks-network/.github
-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.js
80 lines (68 loc) · 1.84 KB
/
server.js
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
require('dotenv').config()
const express = require('express'),
app = express(),
server = require('http').Server(app),
router = express.Router(),
bodyParser = require('body-parser'),
ejwt = require('express-jwt'),
passport = require('passport'),
cors = require('cors'),
port = process.env.PORT || 8000,
fs = require('fs'),
path = require('path'),
_ = require('lodash')
require('./index')
app.use(cors({
origin: true,
methods: ['GET', 'POST', 'PATCH', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization', 'X-Forwarded-For'],
credentials: true
}))
app.use(bodyParser.urlencoded({
limit: '500mb',
extended: true,
type: 'application/x-www-form-urlencoded'
}))
app.use(bodyParser.json({
limit: '500mb',
type: 'application/*'
}))
app.use(passport.initialize())
app.use(function (req, res, next) {
if (req.query.related) {
req.query.related = `[${req.query.related}]`
}
next()
})
function parseQueryString(req, res, next) {
if (req.query && req.query.hasOwnProperty('filter')) {
req.query.filter = _.mapValues(req.query.filter, function (value, key) {
if (value === 'false')
return false
else if (value === 'true')
return true
else
return value
});
}
if (req.query && req.query.hasOwnProperty('filterRelated')) {
req.query.filterRelated = _.mapValues(req.query.filterRelated, function (value, key) {
if (value === 'false')
return false
else if (value === 'true')
return true
else
return value
});
}
next()
}
fs.readdirSync('./app/routes').forEach((file) => {
router.use(`/${path.parse(file).name}`, parseQueryString, require(`./app/routes/${file}`)(
express.Router()
))
})
app.use(router)
server.listen(port, () => {
console.log(`Server active at http://localhost:${port} on ID: ${process.pid}`)
})