-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
67 lines (56 loc) · 1.77 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
const mongoose = require('mongoose');
const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
var db = require('./db/mongoose');
var Customer = require('./models/customer');
var app = express();
const PORT = 3000;
// var john = new Customer(
// firstname: 'John',
// lastname: 'Doe',
// email: '[email protected]',
// phone:'1234567890',
// address: '10 Main Street Columbus Ohio'
// });
app.use(bodyParser.json());
app.post('/customer/create',(req,res)=>{
var customer = new Customer(req.body);
customer.GenerateJWTToken((result)=>{
if(result.status == 'Success'){
res.json(result);
}else{
res.status(400).send(result.ErrorDetails);
}
})
//res.status(200).send('all ok');
})
app.get('/customers',(req,res)=>{
Customer.verifyJWTToken(req.header('X-Auth'))
.then(result =>{
return Customer.find({});
})
.then(result => res.status(200).send(result))
.catch(err => res.status(400).send(err))
});
app.get('/customer/:id',(req,res)=>{
Customer.verifyJWTToken(req.header('X-Auth'))
.then(result =>{
return Customer.findOne({"ID":req.params.id});
})
.then(result => res.status(200).send(result))
.catch(err => res.status(400).send(err));
});
app.patch('/customer/:id',(req,res)=>{
req.body.update_timestamp = Date.now();
Customer.verifyJWTToken(req.header('X-Auth'))
.then(result =>{
return Customer.findOneAndUpdate({"ID":req.params.id},req.body);
})
.then(result => res.status(200).send(result))
.catch(err => res.status(400).send(err));
})
app.listen(PORT,()=>{
console.log('Express listening on port:',PORT);
})