Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
86 lines (75 loc) · 1.9 KB
/
Copy pathserver.js
File metadata and controls
86 lines (75 loc) · 1.9 KB
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
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const ObjectId = require('mongodb').ObjectID;
const path = require('path');
const mongoClient = new MongoClient('mongodb://localhost:27017', {
useNewUrlParser: true
});
const app = express();
app.use(express.json());
app.use(express.static(__dirname + '/dist'));
let dbClient;
mongoClient.connect((err, client) => {
if (err) return console.log(err);
dbClient = client;
app.locals.collection = client.db('usersdb').collection('users');
app.listen(3000, console.log('Сервер запущен 3000'));
});
app.get('/users', (req, res) => {
res.sendFile(path.join(__dirname, './dist', 'index.html'));
})
// GET USERS
app.get('/api/users', (req, res) => {
app.locals.collection.find({}).toArray((err, users) => {
if (err) return console.log(err);
res.json(users);
})
});
// ADD USER
app.post('/api/users', (req, res) => {
if (!req.body) return res.sendStatus(400);
const user = {
name: req.body.name,
age: req.body.age
}
app.locals.collection.insertOne(user, (err, result) => {
if (err) return console.log(err);
res.send(user);
})
});
// EDIT USER
app.put('/api/users', (req, res) => {
if (!req.body) return res.sendStatus(400);
const user = {
id: new ObjectId(req.body.id),
name: req.body.name,
age: req.body.age
}
app.locals.collection.findOneAndUpdate({
_id: user.id
}, {
$set: {
name: user.name,
age: user.age
}
}, {
returnOriginal: false
}, (err, result) => {
if (err) console.log(err);
res.sendStatus(200);
})
});
// DELETE USER
app.delete('/api/users/:id', (req, res) => {
const id = new ObjectId(req.params.id);
app.locals.collection.deleteOne({
_id: id
}, (err, result) => {
if (err) return console.log(err);
res.sendStatus(200);
})
})
process.on("SIGINT", () => {
dbClient.close();
process.exit();
});