ExpressJs Study Note
This is a study note of the first two parts of the book Express in Action.
Express is a routing + sugar layer on top of Node.js HTTP server that simplifies Node APIs. It introduces a plugin framework and adds new functions such as routing and rendering.
A middleware is a small request handler function in Express. It has full access to the request and response parameters. These functions are chained in a pipeline.
Routing examines request method and URL to call different request handleer functions.
Express allows you to break a large application into small subapplicaitons called routers.
let app = express() creates a request handler object and assigns it to
app is both a function and an object that has many methods for.
- routing requests:
postand so on.
- configuring middleware:
- rendering HTML views:
- registering a template engine:
In Node HTTP server, all requests are handled by a request handler funciton that takes a request and a response parameters. Express let you use an array of functions chained together instead of a big one.
app.use(function(req, res, next)) installs a middleware.
There are many third-party Express middlewares such as logger (Morgan), body parser, cookie parser, security and so on.
You can use path in
app.METHOD in a syntax like
who can be accessed as
app.use('my-path', my-router) to setup routers. Each router is like a mini-application that can handle request for a specific path.
4. Extending request and response
Express add methods such as
response.send() and so on to the response parameter.
Express can set views path and view engine.