NodeJS cheatsheet
Syntax snippets and quick notes for revision.
Node Introduction
import express from "express";
const app = express();
app.get("/api/node-introduction", (req, res) => res.json({ ok: true, feature: "Node Introduction" }));
app.listen(3000);Use this node introduction pattern when a NodeJS task needs a small, readable starting point.
Modules
import express from "express";
const app = express();
app.get("/api/modules", (req, res) => res.json({ ok: true, feature: "Modules" }));
app.listen(3000);Use this modules pattern when a NodeJS task needs a small, readable starting point.
NPM
import express from "express";
const app = express();
app.get("/api/npm", (req, res) => res.json({ ok: true, feature: "NPM" }));
app.listen(3000);Use this npm pattern when a NodeJS task needs a small, readable starting point.
File System
import express from "express";
const app = express();
app.get("/api/file-system", (req, res) => res.json({ ok: true, feature: "File System" }));
app.listen(3000);Use this file system pattern when a NodeJS task needs a small, readable starting point.
Events
import express from "express";
const app = express();
app.get("/api/events", (req, res) => res.json({ ok: true, feature: "Events" }));
app.listen(3000);Use this events pattern when a NodeJS task needs a small, readable starting point.
ExpressJS
import express from "express";
const app = express();
app.get("/api/expressjs", (req, res) => res.json({ ok: true, feature: "ExpressJS" }));
app.listen(3000);Use this expressjs pattern when a NodeJS task needs a small, readable starting point.
REST API
import express from "express";
const app = express();
app.get("/api/rest-api", (req, res) => res.json({ ok: true, feature: "REST API" }));
app.listen(3000);Use this rest api pattern when a NodeJS task needs a small, readable starting point.
Middleware
function requireUser(req, res, next) {
if (!req.headers.authorization) return res.status(401).json({ error: "Login required" });
next();
}Use this middleware pattern when a NodeJS task needs a small, readable starting point.
Routing
import express from "express";
const app = express();
app.get("/api/routing", (req, res) => res.json({ ok: true, feature: "Routing" }));
app.listen(3000);Use this routing pattern when a NodeJS task needs a small, readable starting point.
Error Handling
import express from "express";
const app = express();
app.get("/api/error-handling", (req, res) => res.json({ ok: true, feature: "Error Handling" }));
app.listen(3000);Use this error handling pattern when a NodeJS task needs a small, readable starting point.
Authentication
function requireUser(req, res, next) {
if (!req.headers.authorization) return res.status(401).json({ error: "Login required" });
next();
}Use this authentication pattern when a NodeJS task needs a small, readable starting point.
JWT
import express from "express";
const app = express();
app.get("/api/jwt", (req, res) => res.json({ ok: true, feature: "JWT" }));
app.listen(3000);Use this jwt pattern when a NodeJS task needs a small, readable starting point.