Express Js Cheat Sheet



Online Interactive JavaScript (JS) Cheat Sheet. JavaScript Cheat Seet contains useful code examples on a single page. This is not just a PDF page, it's interactive! Find code for JS loops, variables, objects, data types, strings, events and many other categories. Popy-paste the code you need or just quickly check the JS syntax for your projects. Express.js is a minimal web application framework that improves the productivity of web developers. It is very flexible and does not enforce any architecture pattern. This article demonstrates a new architecture pattern which I designed that will further improve your productivity.

Express.js helps abstract the complexities of web services and its Router allows us to modularize our endpoints. This cheat sheet demonstrates how to use Express’s Router for basic RESTful request handling. This is not intended to be a deep dive into writing web services but instead something that can be referenced when learning Express or getting started on a new project. For more in depth study check out the official Express documentation.

Structure

We will be working with two files: server.js and users.service.js. By modularizing our web service with express’s Router we are able to maintain a fairly basic server.js and break down the logic of our api into more manageable modules.

server.js

You might notice an external package in the users service named http-status-codes. This package is not required but I find it useful to have named HTTP status codes. A list of HTTP status codes and their descriptions can be found on Wikipedia. At this point we’re ready to add routes to the user service.

users.service.js

GET Request
Docs

The GET HTTP request method should be used for retrieving data from your service. Below is the most basic GET endpoint you can write. In this example I’m using the HttpStatus package to send back an OK HTTP status code of 200. In addition to the status code we also send a response body which in this example is the string value Request received. Remember that in our server.js we described the users service as having the endpoint /users. This means that in order to use the endpoint below a request would need to be made to http://localhost:1776/users/.

users.service.js

As opposed to other HTTP request methods, GET endpoints should only accept request parameters through the path. In practice, a GET request with input parameters will look something like http://localhost:1776/users/kevinkulp. With just a couple minor changes we can support path parameters in our service.

users.service.js

I want to point out that it is completely legitimate to support both of the endpoints described above: /users and /users/:user_id where each endpoint performs different logic.

DELETE Request

There are a number of other HTTP methods but I’m going to demonstrate one more, DELETE. Handling another HTTP method is as easy as changing the route function used. Unlike GET methods, DELETE endpoints should accept a request body.

users.service.js

Input Validation

In addition to modularizing our web service, another benefit of using Express’s Router is that it allows us to perform validation on input parameters that are used by multiple endpoints. This can also be useful for sanitizing and decoding user input.

users.service.js

Catch All

There are many reasons you might want a catch-all whether it be to perform logging or to authenticate the user. Depending on the location of the catch-all you can regulate which endpoints require authorization. Another way we can control the endpoints that trigger our catch-all is by updating the path used below from * to something like /admin/*.

Express Js Docs

New to Express.js and looking for a handy syntax reference? You can grab this beautifully-designed cheatsheet in high-res PDF form for free. It fits nicely on an 8.5'x11' piece of paper (or A4 for our European friends). Just enter your email address below and we'll send you the cheat sheet!

Here's what you'll find in your inbox:

Express
  • High-res Cheat Sheet PDF
  • Medium-res Cheat Sheet PNG
  • Link to the github repo for the Cheat Sheet
  • Some other cool Express.js resources

Express Js Cheat Sheet

Cheat

Who Built This?

Js Cheat Sheet Pdf

I'm Chris Buecheler, the founder of CloseBrace, a full-stack JavaScript training site. I've been working with Node and Express since 2013. In fact, this very website runs on them. I created this cheat sheet because I thought it would be a handy resource not just for new users, but also for practiced hands who, if they're anything like me, occasionally need a refresher on syntax. Whatever your expertise level, I hope it's useful for you!





Comments are closed.