-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
63 lines (50 loc) · 2.34 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
const express = require('express');
const app = express();
var mongoose = require('mongoose');
const doctorRoutes = require('./routes/doctorRoutes');
const patientRoutes = require('./routes/patientRoutes')
const {loginRequired} = require('./controllers/authController')
const bodyParser = require('body-parser')
const listEndpoints = require('express-list-endpoints');
const {login, register} = require('./controllers/authController');
const jwt = require('jsonwebtoken');
//Set up default mongoose connection
//mongodb://dbuser:[email protected]:27017,mongo-cluster-shard-00-01-ixqtu.mongodb.net:27017,mongo-cluster-shard-00-02-ixqtu.mongodb.net:27017/test?ssl=true&replicaSet=mongo-cluster-shard-0&authSource=admin&retryWrites=true
//var mongoDB = 'mongodb+srv://pranav:[email protected]/<local_library>?retryWrites=true&w=majority';
//var mongoDB ='mongodb://pranav:[email protected]:27017,mongo-cluster-shard-00-01-ixqtu.mongodb.net:27017,mongo-cluster-shard-00-02-ixqtu.mongodb.net:27017/test?ssl=true&replicaSet=mongo-cluster-shard-0&authSource=admin&retryWrites=true'
//var mongoDB = 'mongodb+srv://pranav:[email protected]/test?retryWrites=true&w=majority';
var mongoDB='mongodb+srv://pranav:[email protected]/local_library?retryWrites=true&w=majority';
// Connect the mongoose with database
mongoose.connect(mongoDB, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.then(()=>{
console.log('connect succesfully!');
})
.catch(error => console.log(error));
app.use(bodyParser.json());
app.use(async (req, res, next) => {
if (req.headers && req.headers.authorization && req.headers.authorization.split(' ')[0]==='JWT') {
token = req.headers.authorization.split(' ')[1];
jwt.verify(token, 'secretKey', (err, decoded) => {
err? res.json(err.message) : req.user = decoded;
next();
} );
} else {
req.user = undefined;
next();
}
});
//login route
app.post('/auth/login', login);
app.post('/auth/register', register);
//login route
app.use('/doctors', loginRequired, doctorRoutes)
app.use('/patients', loginRequired, patientRoutes)
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`listening on port ${PORT}`);
console.log(listEndpoints(app));
})