-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
58 lines (41 loc) · 1.68 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
const express = require('express');
const app = express();
const http = require('http');
const server = http.Server(app);
const PORT = process.env.PORT || 81;
const expressValidator = require('express-validator');
const expFileUpload = require("express-fileupload");
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const AuthController = require('./controller/auth.controller');
const opr = require('./helper/operations');
const user = require('./routes/user');
const userType = require('./routes/user_type');
const clientType = require('./routes/client_type');
app.use(expressValidator());
app.use(expFileUpload());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(session({secret: 'REST-API', saveUninitialized: false, resave: false}));
app.use(methodOverride(function (req, res) {
if (req.body && typeof req.body === 'object' && '_method' in req.body) {
const method = req.body._method;
delete req.body._method;
return method
}
}));
const token = (req, res, next)=>{
AuthController.getAuthToken(req,res,next);
};
const auth = (req, res, next)=>{
AuthController.verifyAuth(req, res, next);
};
app.all('/', token,(req, res) => res.status(200).send(opr.returnSuccess(req.auth)));
app.use('/v1/user', auth, user);
app.use('/v1/user_type', auth, userType);
app.use('/v1/client_type', auth, clientType);
app.all('/test', (req, res)=> res.status(403).send(opr.returnSuccess("HERE")));
server.listen(PORT, () => console.log(`REST-API Port running on ${PORT}!`));