-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
42 lines (31 loc) · 988 Bytes
/
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
const express = require('express');
const cors = require('cors');
const { json } = require('body-parser');
const MongoClient = require("mongodb").MongoClient;
const server = express();
const mongoClient = new MongoClient("mongodb://127.0.0.1:27017/");
server.use(cors());
server.use(json());
server.post('/registration/', async (request, response) => {
const login = request.body.login;
const password = request.body.password;
await mongoClient.connect();
const db = mongoClient.db("l2db");
const isExist = await db.collection('accounts').findOne({ login });
if (isExist) {
response.send({
status: 'failed'
});
return
}
const accountsIds = await db.collection('accounts').find({}, { projection: { _id: 0, id: 1 } }).sort({ id: 1 }).toArray();
await db.collection('accounts').insertOne({
id: accountsIds[accountsIds.length - 1].id + 1,
login,
password
});
response.send({
status: 'success'
});
});
server.listen(80);