-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (101 loc) · 2.8 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const express = require('express');
const bodyParser= require('body-parser')
const app = express();
let uniqueID = 0;
let users = [];
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");
res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.listen(3000, function() {
console.log('listening on 3000');
});
app.get('/', function(req, res) {
res.send("Yep it's working");
});
app.post('/users', (req, res, next) => {
let user = {
firstName: req.body.user.firstName,
lastName: req.body.user.lastName
};
user.id = uniqueID++;
users.push(user);
res.send({user:user});
// dbase.collection("name").save(name, (err, result) => {
// if(err) {
// console.log(err);
// }
//
// res.send('name added successfully');
// });
});
app.get('/users', (req, res) => {
console.log('yipuieee get');
res.send({user:users});
// dbase.collection('name').find().toArray( (err, results) => {
// res.send(results)
// });
});
app.get('/users/:id', (req, res, next) => {
// if(err) {
// throw err;
// }
let id = req.params.id;
let userIndex = users.findIndex((data)=>{
return data.id=== parseInt(id);
});
if(userIndex>-1){
res.send({user:users[userIndex]});
}else{
res.send('user doesnt exists');
}
// dbase.collection('name').find(id).toArray( (err, result) => {
// if(err) {
// throw err;
// }
//
// res.send(result);
// });
});
app.put('/users/:id', (req, res, next) => {
let id = req.params.id;
let userIndex = users.findIndex((data)=>{
return data.id===parseInt(id);
});
if(userIndex>-1){
users[userIndex].firstName = req.body.user.firstName;
users[userIndex].lastName =req.body.user.lastName;
res.send({user:users[userIndex]});
}else{
res.send('user doesnt exists');
}
// dbase.collection("name").update({_id: id}, {$set:{'firstName': req.body.firstName, 'lastName': req.body.lastName}}, (err, result) => {
// if(err) {
// throw err;
// }
// res.send('user updated sucessfully');
// });
});
app.delete('/users/:id', (req, res, next) => {
let id = req.params.id;
let userIndex = users.findIndex((data)=>{
return data.id=== parseInt(id);
});
if(userIndex>-1){
users.splice(userIndex,1);
res.send({});
}else{
res.send('user doesnt exists');
}
// dbase.collection('name').deleteOne(id, (err, result) => {
// if(err) {
// throw err;
// }
//
// res.send('user deleted');
// });
});