-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
108 lines (96 loc) · 3.73 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
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
103
104
105
106
107
108
let express = require('express');
let mongo = require('mongodb');
let MongoClient = mongo.MongoClient;
let app = express();
let cors = require("cors");
let bodyParser = require('body-parser');
let cookieParser = require('cookie-parser');
let TokenHandler = require('./tokenHandler').TokenHandler;
app.use(bodyParser.json());
app.use(cookieParser());
app.use(cors());
app.use("/images", express.static(__dirname + "/images/"));
app.listen(3000, function () {
console.log("Served is running")
});
app.get("/sayHello", function (request, response) {
response.send("Hello");
});
app.get("/sayHelloWithStatus", function (request, response) {
response.status(200).send("Hello, with status 200");
});
MongoClient.connect("mongodb://127.0.0.1:27017", {userNewUrlParser: true}, function (err, client) {
if (err) {
console.log("error with db");
return;
}
console.log("connected to db");
let tokenLifetime = 300;
let db = client.db("config"); //connected to db Shop
let products = db.collection("Items"); //connected to collection(table) in Shop db
let clients = db.collection("Clients");
let tokenHandler = new TokenHandler("my_secret_key", tokenLifetime, clients);
app.get("/showProducts", function (request, response) {
let searchQuery = {};
if (request.query.type !== undefined) {
if (request.query.type !== "") {
searchQuery["type"] = request.query.type;
}
}
//search function
if (request.query.name !== undefined) {
if (request.query.name !== "") {
searchQuery["name"] = {$regex: request.query.name, $options: 'i'}
}
}
products.find(searchQuery).toArray(function (err, result) {
if (err) {
response.status(200).send("something wrong with db");
} else {
let jsonResult = JSON.stringify(result);
response.status(200).send(jsonResult);
}
})
}
);
app.get('/signIn', (request, response) => {
if (request.query.name === undefined || request.query.name === "") {
response.status(409).send("No username provided");
return;
}
if (request.query.password === undefined || request.query.password === "") {
response.status(409).send("No password provided");
return;
}
let name = request.query.name;
let password = request.query.password;
tokenHandler.signIn(name, password, (token) => {
if (!token){
response.status(403).send("Not authorized");
} else {
response.cookie('token', token, { maxAge: tokenLifetime * 1000 }); //put token in browser's cookies
response.status(200).send(token); //
}
})
});
app.get('/register', (request, response) => {
if (request.query.name === undefined || request.query.name === "") {
response.status(409).send("No username provided");
return;
}
if (request.query.password === undefined || request.query.password === "") {
response.status(409).send("No password provided");
return;
}
let name = request.query.name;
let password = request.query.password;
tokenHandler.registerUser(name, password, (result) => {
if (!result){
response.status(403).send("User already exists");
} else {
response.cookie('token', result, { maxAge: tokenLifetime * 1000 });
response.status(200).send(result);
}
})
});
});