-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
31 lines (25 loc) · 812 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
const express = require("express")
const app = express()
const port = 3000
app.use(express.json())
app.use((request, response, next) => {
console.log("Request received", request.body)
next()
})
app.post('/', (request, response) => {
if (!request.body.hasOwnProperty("foo"))
throw new Error("`foo` expected in body")
if (request.body.foo == "bar") {
console.log("`foo-bar` matched. We likes it!")
response.status(200).send(request.body)
}
else {
console.log("Expected value for `foo` is `bar`. Received : ", request.body.foo)
throw new Error("`foo` was sent with wrong value. We dislikes it!")
}
})
app.use((err, request, response, next) => {
console.log(err)
response.status(500).send({ error: err.toString()})
})
app.listen(port)