-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
85 lines (71 loc) · 2.33 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
81
82
83
84
85
/*
Imports
*/
// Node
require('dotenv').config(); //=> https://www.npmjs.com/package/dotenv
const express = require('express'); //=> https://www.npmjs.com/package/express
const path = require('path'); //=> https://www.npmjs.com/package/path
const bodyParser = require('body-parser'); //=> https://www.npmjs.com/package/body-parser
// Inner
const MongoClass = require('./services/mongo.class')
const PostModel = require('./models/post.model');
//
/*
Server definition
*/
class ServerClass{
// Inject properties in the ServerClass
constructor(){
this.server = express();
this.port = process.env.PORT;
this.mongDb = new MongoClass();
}
init(){
// Static path configuration
this.server.set( 'views', __dirname + '/www' );
this.server.use( express.static(path.join(__dirname, 'www')) );
// Set server view engine
this.server.set( 'view engine', 'ejs' );
//=> Body-parser
this.server.use(bodyParser.json({limit: '20mb'}));
this.server.use(bodyParser.urlencoded({ extended: true }));
// Start config
this.config();
}
config(){
// TODO: set auth route
// Set up API router
const ApiRouterClass = require('./router/api.router');
const apiRouter = new ApiRouterClass();
this.server.use('/api', apiRouter.init());
// Set up Backoffice router
const BackRouterClass = require('./router/backoffice.router');
const backRouter = new BackRouterClass();
this.server.use('/', backRouter.init());
// Start server
this.launch();
}
launch(){
// Connect MongoDB
this.mongDb.connectDb()
.then( db => {
// Start server
this.server.listen( this.port, () => {
console.log({
node: `http://localhost:${this.port}`,
db: db.url,
})
})
})
.catch( dbError => {
console.log(dbError)
})
}
}
//
/*
Start server
*/
const MyServer = new ServerClass();
MyServer.init();
//