-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
69 lines (62 loc) · 2.41 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
require('dotenv').config();
const express = require('express');
var login = require('./routes/#routes');;
var query = require('./routes/queryroutes')
const bodyParser = require('body-parser');
const pool = require('./db').pool;
const jwt = require('jsonwebtoken');
pool.getConnection((err, testconn) => {
if(err) console.error('database connection error');
else{
console.log('database connected');
testconn.query('SELECT COUNT(*) AS recordcount FROM Users;', (err, result, fields) => {
if(err) console.error('record count query error');
else console.log(`${result[0]['recordcount']} records in Users`);
});
testconn.query('SELECT COUNT(*) AS recordcount FROM UserRecords;', (err, result, fields) => {
if(err) console.error('record count query error');
else console.log(`${result[0]['recordcount']} records in UserRecords`);
});
}
})
const verifyToken = () => {
return (req, res, next) => {
const authHeader = req.headers['authorization']
const token = authHeader && authHeader.split(' ')[1]
if (token == null) res.send({
'code':401,
'error':'no authorization token'
})
jwt.verify(token, process.env.TOKEN_SECRET, (err, data) => {
if(err){
console.log(err);
res.send({
'code':403,
'error':'error verifying token'
})
} else{
req.id = data.ID;
next();
}
})
}
}
const app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(function(req, res, next){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
var router = express.Router();
router.post('/register', login.register);
router.post('/#', login.login);
router.post('/search', verifyToken(), query.search);
router.post('/stats', query.stats);
router.post('/createRequest', verifyToken(), query.createRequest);
router.post('/getRequests', verifyToken(), query.getRequests);
router.post('/approveRequest', verifyToken(), query.approveRequest);
router.post('/updateProfile', verifyToken(), query.updatePassword, query.updateProfile);
app.use('/api', router);
app.listen(907, () => console.log('Server running.'));