Skip to content

Commit 9f5ad6b

Browse files
Including http-status-code as base codes
1 parent 5e23db7 commit 9f5ad6b

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed

index.js

+30-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
const express = require('express')
22
const app = express()
3-
const port = process.env.PORT || 3000;
3+
const port = process.env.PORT || 3000
4+
const HTTPStatusCode = require('http-status-code')
5+
var statusId = 200
6+
var allStatus = HTTPStatusCode.getProtocolDefinitions(statusId)
47

8+
// Points to API URL, based on platform (local or Heroku)
59
app.get('/', (req, res) => {
610
var hostName = req.hostname
711

@@ -16,14 +20,35 @@ app.post('/', (req, res) => {
1620
res.send(`Redirect to ${req}`)
1721
})
1822

23+
/**
24+
* TODO:
25+
* 1. if receive Status Code as /api/400 returns this status code
26+
* else, returns random status code
27+
* 2. when returns some status code, include some text message and an image uri
28+
*/
1929
app.get('/api', (req, res) => {
30+
statusId = 200;
2031
res.json(
21-
[
22-
{'message': 'Return to API request'}
23-
]
24-
)
32+
{'message': 'Return to API request: ' + statusId, allStatus}
33+
)
2534
})
2635

36+
app.param('id', function (req, res, next, id) {
37+
statusId = id
38+
next()
39+
})
40+
41+
app.get('/api/:id', function (req, res) {
42+
res.json(
43+
{
44+
'message': 'Return to API request: ' + statusId,
45+
'status': HTTPStatusCode.getMessage(statusId)
46+
}
47+
)
48+
res.end()
49+
})
50+
51+
// Indicate running app
2752
app.listen(port, () => {
2853
console.log(`Our app is running on port ${ port }`);
2954
});

package-lock.json

+34-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"author": "Fabio Beneditto",
1010
"license": "MIT",
1111
"dependencies": {
12-
"express": "^4.17.3"
12+
"express": "^4.17.3",
13+
"http-status-code": "^2.1.0"
1314
}
1415
}

0 commit comments

Comments
 (0)