-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
53 lines (42 loc) · 1.14 KB
/
App.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
const fs = require('fs');
var express = require('express');
var app = express();
app.use(express.json());
const tours = JSON.parse(fs.readFileSync(`${__dirname}/dev_data/tours-simple.json`));
app.get('/api/v1/tours', (req, res) => {
res.status(200).json({
status: 'success',
results:tours.length,
data: {
tours
}
});
});
app.get('/api/v1/tours/:id', (req, res) => {
const id=req.params.id*1;
const tour=tours.find(el=> el.id ===id)
res.status(200).json({
status: 'success',
data:{
tour
}
});
});
app.post('/api/v1/tours', (req,res)=>{
// console.log(req.body);
const newId=tours[tours.length-1].id+1;
const newTour=Object.assign({id:newId},req.body);
tours.push(newTour);
fs.writeFile(`${__dirname}/dev_data/tours-simple.json`, JSON.stringify(tours),err=>{
res.status(201).json({
status:'success',
data:{
tour:newTour
}
});
} );
// res.send('DONE');
})
var server = app.listen(5000, function () {
console.log('Node server is running..');
});