Node.js lets JavaScript run outside the browser, so students can build servers, APIs, command-line tools, and full-stack applications. Express is a small web framework for Node.js that makes routing, middleware, and HTTP responses easier to organize. This cheat sheet gives students a quick reference for the commands, patterns, and code structure used in beginner server projects.
It is useful when starting a new app, debugging routes, or reviewing how requests move through a server.
Key Facts
- Run a JavaScript file with Node using the command node filename.js.
- Start a new Node project with npm init -y, which creates a package.json file with default settings.
- Install Express with npm install express, then import it with const express = require('express').
- Create an Express app with const app = express(), then start the server with app.listen(PORT, callback).
- A basic GET route uses app.get('/path', (req, res) => { res.send('message'); }).
- Use app.use(express.json()) so Express can read JSON data sent in the request body.
- Common CRUD methods are GET for read, POST for create, PUT or PATCH for update, and DELETE for remove.
- Common HTTP status codes include 200 OK, 201 Created, 400 Bad Request, 404 Not Found, and 500 Server Error.
Vocabulary
- Node.js
- Node.js is a runtime that allows JavaScript code to run on a server or computer outside a web browser.
- npm
- npm is the Node package manager used to install libraries, run scripts, and manage project dependencies.
- Express
- Express is a lightweight Node.js framework that helps create web servers, APIs, routes, and middleware.
- Route
- A route is a rule that connects an HTTP method and URL path to a function that handles the request.
- Middleware
- Middleware is a function that runs between the incoming request and the final route response.
- CRUD
- CRUD stands for create, read, update, and delete, which are the four basic operations used to manage data.
Common Mistakes to Avoid
- Forgetting app.use(express.json()) is wrong because req.body will be undefined when the client sends JSON data.
- Putting a catch-all route before specific routes is wrong because Express checks routes in order and may stop before reaching the intended handler.
- Using the wrong HTTP method is wrong because app.get('/users') and app.post('/users') are different routes with different purposes.
- Sending more than one response in the same route is wrong because each request should receive only one final response, such as res.send() or res.json().
- Hard-coding a port without a fallback is risky because hosted apps often require process.env.PORT, so use const PORT = process.env.PORT || 3000.
Practice Questions
- 1 Write the terminal commands to create a new Node project and install Express.
- 2 Given const PORT = 3000, write a short app.listen statement that starts the server and logs Server running.
- 3 Write an Express route for GET /health that sends the JSON response {"status":"ok"}.
- 4 Explain why middleware order matters in an Express app, especially when using express.json(), route handlers, and error handlers.