forked from chambo-e/HomeComix-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (43 loc) · 1.62 KB
/
server.js
File metadata and controls
51 lines (43 loc) · 1.62 KB
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
const express = require('express')
const bodyParser = require('body-parser')
const router = require('./routes/router.js')
const database = require('./database.js')
const account = require('./account/manager.js')
const success = '\x1b[32mOK\x1b[0m'
const failure = '\x1b[31mFAILED\x1b[0m'
const homecomixApi = '[\x1b[35mHomeComix-API\x1b[0m] '
const app = express()
// Port setting
const port = process.env.PORT || 3000
// Allows nested object
app.use(bodyParser.urlencoded({extended: true}))
// Parses incoming request bodies in a middleware
app.use(bodyParser.json())
// Account management
app.post('/api.homecomix/register', account.register)
app.post('/api.homecomix/authorize', account.authorize)
app.delete('/api.homecomix/delete', account.delete)
// Middlewares
// Ensures that all requests starting with /api.homecomix/:uid/* will be checked
// for the token
// TODO: remove :uid from middleware init
// app.all('/api.homecomix/*', [require('./middleware/token')])
app.all('/api.homecomix/:uid/*', [require('./middleware/token')])
// Mounts the router as middleware at path "/"
app.use('/', router)
// Starts the server
app.listen(port, () => {
console.log(`${homecomixApi}Server listening on port ${port} [${success}]`)
database.connect(err => {
if (err) {
console.log(`${homecomixApi}Connection to the database [${failure}]`)
throw (err)
} else {
console.log(`${homecomixApi}Connection to the database [${success}]`)
database.init().then(result => {
const res = result === true ? success : failure
console.log(`${homecomixApi}HomeComix starting service [${res}]`)
})
}
})
})