-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
56 lines (46 loc) · 1.3 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
// Import the express library here
const express = require ('express');
// Instantiate the app here
const app = express();
const PORT = process.env.PORT || 3000;
// function getElementById(num, resource){
// resource.forEach(element => {
// if (num = element.id){
// console.log(element);
// console;log(element.id);
// return element;
// }
// else{
// res.status(404).send();
// }
// });
// }
// Import and mount the datarouter
const datarouter = require('./database/datab');
const data = require('./database/datab');
const { json } = require('body-parser');
app.get("/allProducts", function (req, res) {
const broughtDataB = data;
if (broughtDataB) {
res.send(broughtDataB);
} else {
res.status(404).send();
}
});
app.get('/OneProduct/:id', (req, res, next) => {
let idt = req.params.id;
// res.send(idt);
console.log(typeof(idt));
// console.log(foundObject);
const foundObject = data.find(item => item.id == idt);
if (foundObject) {
res.send(foundObject);
console.log(foundObject);
} else {
res.status(404).send();
}
});
// Invoke the app's `.listen()` method below:
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});