-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (32 loc) · 966 Bytes
/
index.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
const express = require('express')
const fs = require('fs')
const app = express()
app.use(express.json())
const hotels = JSON.parse(fs.readFileSync(`${__dirname}/dev-data/data/hotels-simple.json`))
app.get('/api/v1/hotels/:id', (req, res) => {
const id = req.params.id * 1
const hotel = hotels.find(el => el.id === id)
res.status(200).json({
status: 'success',
data: {
hotel
}
})
})
app.post('/api/v1/hotels', (req, res) => {
const newId = hotels[hotels.length-1].id + 1
const newHotel = Object.assign({id: newId}, req.body)
hotels.push(newHotel)
fs.writeFile(`${__dirname}/dev-data/data/hotels-simple.json`, JSON.stringify(hotels), err => {
res.status(201).json({
status: 'success',
data: {
hotels: newHotel
}
})
})
})
const port = 3000
app.listen(port, () => {
console.log(`App is running on port ${port}...`)
})