This repository has been archived by the owner on Sep 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
61 lines (50 loc) · 1.42 KB
/
index.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
/* Require Modules */
const express = require('express')
const bParser = require('body-parser')
const morgan = require('morgan')
const util = require('./util/Util')
/* Require Routes */
const auth = require('./routes/Auth.js')
const blog = require('./routes/Blog.js')
const user = require('./routes/User.js')
/* Our App! */
const app = express()
/* Basic Middlewares */
app.use(bParser.json())
app.use(bParser.urlencoded({ extended: true }))
app.set('json spaces', 4)
/* Logging */
const loggerMode = util.useLogger(process.env.NODE_ENV)
if (loggerMode) {
app.use(morgan(loggerMode))
}
/* Routes */
// Enable CORS, Set Response Type as JSON
app.use(function (_req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Content-Type', 'application/json; charset=utf-8')
next()
})
// Attach Mongo URLs to req object
app.use(function (_req, res, next) {
res.locals.mongoUri = util.getMongoUrl(process.env.NODE_ENV)
next()
})
// Application Routes
app.use('/auth', auth)
app.use('/blog', blog)
app.use('/user', user)
// Render any other route than the ones defined anywhere in app as HTTP 404
app.use(function (_req, res) {
res.status(404).send({
Message: '',
DocsURL: 'https://sakshamsaxena.github.io/uzay'
})
})
/* Listen only when not running Tests */
if (util.startServer(process.env.NODE_ENV)) {
app.listen(3000, function () {
console.log('Uzay live on port 3000!')
})
}
module.exports = app