-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.js
73 lines (63 loc) · 2.21 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
require('dotenv').config()
const express = require('express')
const rateLimit = require('express-rate-limit')
const app = express()
const cons = require('consolidate')
const bodyParser = require('body-parser')
const path = require('path')
const morgan = require('morgan')
const mongoose = require('mongoose')
// Rate Limiter
const limiter = rateLimit({
max: 100,
windowMs: 60 * 1000,
message: 'To Many request from this IP, please try again in an hour!'
})
app.use('/v2', limiter)
// Middlewares
app.use(morgan('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
app.use('/assets', express.static('assets'))
// Routers
const animalRouter = require('./Src/Router/Animals')
const otherRouter = require('./Src/Router/Others')
const textRouter = require('./Src/Router/Text')
const memeRouter = require('./Src/Router/Meme')
const viewRouter = require('./Src/Router/View')
const imageRouter = require('./Src/Router/Images')
const gameRouter = require('./Src/Router/Games')
const adminRouter = require('./Src/Router/admin')
const urlRouter = require('./Src/Router/urlshort')
const fakeRouter = require('./Src/Router/fake')
app.use('/', viewRouter)
app.use('/v2/animals', animalRouter)
app.use('/v2/others', otherRouter)
app.use('/v2/text', textRouter)
app.use('/v2/meme', memeRouter)
app.use('/v2/images', imageRouter)
app.use('/v2/games', gameRouter)
app.use('/v2/admin', adminRouter)
app.use('/v2/shorturl', urlRouter)
app.use('/v2/fake', fakeRouter)
// v1 not supported error
app.use('/v1*', async (req, res, next) => {
res.status(404).render('v1Dep')
})
// 404 error
app.use('*', async (req, res, next) => {
res.status(404).render('404')
})
// Connection to server and mongoDB
app.listen(process.env.PORT || 4000, async () => {
console.log(`===> The API is running on Port ${process.env.PORT || 4000}...`)
await mongoose.connect(process.env.MONGOSRV, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
}).then(async () => {
console.log('===> Connected to MongoDB (Data)')
}).catch((err) => { console.log(err) })
})