Skip to content

Instantly share code, notes, and snippets.

View chsnt's full-sized avatar
🖍️

chsnt

🖍️
  • Moscow
View GitHub Profile

Middleware

Middleware refers to modules that are chained 'between' when your Express app is instantiated and when your server is launched with app.listen(myPortNumber).

We'll stick to two easy examples that illustrate the usefulness of middleware: logging and static resources.

Registering and Using Middleware

Using middleware is simple. Use Node.js to require the package that you'd like to use. Then add the middleware to your app by calling it with .use(). The order that you call your middleware may matter, so look in the docs for your middleware to make sure it's well-positioned.

Here's how we would deploy the logging middleware Morgan.

Simplify Your Life with Express Routers

So far we've handled our requests and responses with ease. But we still haven't seen how Express solves the annoying problem of routing the many resources and pages that a modern website may have.

Routing to Sub-Resources

Let's imagine that our website has a sub-page dedicated to a collection of kittens at '/kittens'.

Easy, right? As we saw before, we could just add a route to our app like this:

      app.get('/kittens', function(request, response, next) {
@revett
revett / git_branch_naming.md
Last active December 22, 2023 17:12
Git Branch Naming Conventions

Syntax

<author>/<type>/<ticket>/<title>

Example

revett/feature/24101/skeleton-service-for-email-sender