-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (33 loc) · 1.26 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
const restify = require('restify');
const mongoose = require('mongoose');
const restify_jwt = require('restify-jwt-community');
const corsMiddleware = require('restify-cors-middleware')
require('dotenv').load();
const server = restify.createServer();
server.use(restify.plugins.bodyParser());
const cors = corsMiddleware({
preflightMaxAge: 5, //Optional
origins: ['*'],
allowHeaders: ['API-Token' , 'Authorization','Access-Control-Allow-Origin'],
exposeHeaders: ['API-Token-Expiry'],
})
server.pre(cors.preflight)
server.use(cors.actual)
// protect all routes unless registration and login entry point
// server.use(restify_jwt({secret: process.env.JWT_SECRET}).unless({path:['/auth']}));
// when server listen connect to the data base
server.listen(process.env.PORT || 5000, () => {
mongoose.set('useFindAndModify', false);
mongoose.connect(process.env.MONGODB_URI, {useNewUrlParser: true});
});
const db = mongoose.connection;
db.on('error', (error) => {
console.log(error)
});
// if we have connection opened then require route file
db.on('open', () => {
require('./routes/user')(server);
require('./routes/group')(server);
require('./routes/chat')(server);
console.log(`server start on port ---> ${process.env.PORT}`);
});