-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.js
More file actions
70 lines (59 loc) · 1.95 KB
/
app.js
File metadata and controls
70 lines (59 loc) · 1.95 KB
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
/*
Example recipes web API built using node and express
*/
let express = require('express')
let app = express()
let port = 3000
let recipes = []
let bodyParser = require('body-parser')
let uuidv4 = require('uuid/v4')
// allows for the parsing request.body in different formats
// application/json:
app.use(bodyParser.json())
// request format for POST requests from a HTML form
// application/x-www-form-urlencoded:
app.use(bodyParser.urlencoded({ extended: true }))
// enables CORS on the client via response headers
app.use((request, response, next) => {
response.header("Access-Control-Allow-Origin", "*")
response.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
next()
})
// HOME
app.get('/', (request, response) => {
response.send('Hello World')
console.log(response)
})
// CREATE a data entry with a POST http request
app.post('/recipes', (request, response) => {
console.log(request.body)
response.send("CREATE: new recipe")
})
// READ all data with a GET
app.get('/recipes/all', (request, response) => {
response.send('READING all recipes from the DB')
console.log(response)
})
// UPDATE a data entry with a POST
app.post('/recipes/update/:id', (request, response) => {
let id = request.params.id
console.log(`UPDATE: recipe id ${id}`)
})
// alternative using the UPDATE http verb (not as well supported as POST in clients)
// app.update('/recipes/:id', (request, response) => {
// let id = request.params.id
// console.log(`UPDATE: recipe id ${id}`)
// })
// DELETE a data entry with a POST
app.post('/recipes/delete/:id', (request, response) => {
let id = request.params.id
console.log(`DELETE: recipe id ${id}`)
})
// alternative using the DELETE http verb (not as well supported as POST in clients)
// app.delete('/recipes/:id', (request, response) => {
// let id = request.params.id
// console.log(`DELETE: recipe id ${id}`)
// })
app.listen(port, () =>{
console.log(`Express server listening on port ${port}`)
})