- 🧶 Create application;
const hpp_http = require("./index");
const app = hpp_http.createApplication();
- 🧶 GET route
app.get("/test", (req, res) => {
console.log(req.headers);
res.status(200).send({ api: true });
});
- 🧶 POST route
app.post("/test_post", (req, res) => {
console.log(req.headers);
const bodyparam = req.body['body-param'];
res.status(200).send({ bodyparam });
});
- 🧶 Use JSON Content-Type or your custom Content-Type
app.use(hpp_http.hppJson);
app.use(hpp_http.hppCustomContentType("your-custom-Content-Type"))
- 🧶 Use required param in POST method
app.post("/test_post_required", (req, res) => {
const param_x = req.body['param-x'];
const param_y = req.body['param-y'];
const param_z = req.body['param-z'];
req.require(['param-x', 'param-y', 'param-z']);
res.status(200).send({ param_x, param_y, param_z });
});
- 👉 if someone required param is null ->
- 🧶 Use required header param in GET method
app.get("/test_require_header", (req, res) => {
console.log(req.headers);
const header_x = req.headers['header-x'];
req.requireHeader(['header-x']);
res.status(200).send({ header_x });
});
- 👉 if someone required header param is null ->
- 🧶 Use requiredType body param in POST method
app.post("/test_requireType", (req, res) => {
const param_x = req.body['param-x'];
req.require(["param-x"]);
req.requireType([
["param-x", "string"] // param_x must be a string
]);
res.status(200).send({ param_x });
});
-
🍄 Options are:
string
,number
,bool
,object
,array
-
👉 if someone requiredtype body param is different to be expected ->
- 🧶 Start Server
app.listen(_port, callback);