We've set everything up; now, let's see our code working:
> npm run build
> node out/middleware.js
We can use some curl requests to test this; let's use GET, POST, and DELETE:
> curl "http://127.0.0.1:8080/some/path/to/get?value=9"
Server alive, with Express!
> curl -X POST "http://127.0.0.1:8080/a/post/to/a/path"
Server alive, with Express!
> curl -X DELETE "http://127.0.0.1:8080/try/to/delete?key=22"
INTERNAL SERVER ERROR
The logged output will be as follows:
Logger... 2018-05-08T00:22:20.192Z GET /some/path/to/get
Logger... 2018-05-08T00:22:44.282Z POST /a/post/to/a/path
Logger... 2018-05-08T00:23:01.888Z DELETE /try/to/delete
Error.... DELETEs are not accepted!
Now, we now know how to write our own middleware, but it so happens that Express provides lots of ready-made functions. Let's give them a whirl and look at how we can use them for several common needs.