I don't often create express back-ends so I made this cheat sheet to refresh my memory of how to do it. This cheat sheet contains modules that show how to do a bunch of things with Express (request handling, mongoDB, registration, authorization, etc), with lots of comments.
Outlines how create endpoints to handle GET, POST, PUT and DELETE requests.
Run node 1_basic_request_handling.js
and use 1_basic_request_handling.http
to test the endpoints.
Brief overview of built-in, third party and custom middleware functions.
Brief overview of how to use the debug
npm package to debug. This is an alternative to using console.log
for debugging.
Outlines how to connect to a mongoDB database on the computer via mongoose. Also outlines how to:
- Save data
- Query the database
- Update documents
- Delete documents
Outlines how to use mongoose to validate data before saving it to the database. Includes examples of built-in validators, custom validators and other useful properties.
Brief overview of the different approaches to establishing relationships between documents.
Example of how to establish relationships between documents via the referencing approach.
Example of how to establish relationships between documents via the embedding approach.
Brief overview of mongoDB object Ids and how you can extract the timestamp from them and validate them.
Example of how to setup a database connection and registration endpoint to register users and save them to the database. Also shows how to hash passwords and create and send JSON web tokens.
Run node 8.1_account_registration.js
and use 8.1_account_registration.http
to register users and save them to your database.
Example of how to setup a database connection and login endpoint to login with details that are saved on the database. Run node 8.1_account_registration.js
and use 8.1_account_registration.http
to register an account first. Also shows how to generate and send back a JSON web token.
Run node 8.2_user_login.js
and use 8.2_user_login.http
to test the login endpoint.
Overview of how to protect endpoints using JSON web tokens. Includes an example of an endpoint that can only be accessed by users who are logged in (send a valid JSON web token)
Run node 8.3_protecting_routes.js
and use 8.3_protecting_routes.http
to test the protected and unprotected endpoints.
Brief overview of how a logging out function could be implemented.
Example of how a simple express server can be structured, with separate folders and modules for routes and models.
Run node 9_index.js
and use 9_index.http
to test the various endpoints.