Manage your learning questions and answers
Define a final error handler with four args (err, req, res, next).
app.use((err, req, res, next) => {
res.status(500).json({ error: "Internal error" });
});
Read configuration from .env in development and access via process.env.
require("dotenv").config();
const dbUrl = process.env.DATABASE_URL;
Use useEffect plus fetch (or a data library). In the App Router, prefer Server Components for data when possible.
useEffect(() => {
fetch("/api/data")
.then((r) => r.json())
.then(setData);
}, []);
useMemo caches expensive computations between renders. Only use it for actual cost or stable references.
const total = useMemo(() => items.reduce((s, n) => s + n, 0), [items]);
Use the Link component to avoid full page reloads and keep client-side transitions.
import Link from "next/link";
<Link href="/courses">Courses</Link>;
Middleware are functions that have access to req/res and next; used for auth, logging, validation, etc.
Use a Client Component when you need browser-only APIs (local state, effects, event handlers). Otherwise, prefer Server Components for performance.
Both describe shapes. Interfaces are extendable/mergeable; type aliases are more flexible for unions/intersections.
Use express(), define routes, and listen on a port.
const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('OK'));
app.listen(3000);
Return a function from useEffect to clean subscriptions/timers. React calls it before the effect re-runs or during unmount.
useEffect(() => {
const id = setInterval(() => console.log("tick"), 1000);
return () => clearInterval(id);
}, []);
number, string, boolean, null, undefined, symbol, bigint, arrays, tuples, and object types.
The event loop processes queued callbacks and enables non-blocking I/O in Node.js.