Node.js
Project design
Design the skeleton of the intermediate final mini app.
Project: Mini API server
In this 5-lesson project you will build a mini API server with Express. Each lesson adds one piece, and by the final lesson routes, file persistence and middleware work together. Each lesson's starting code contains the previous lesson's solution.
Express is the most common framework for writing HTTP servers on Node.js. You import it with const express = require('express') and create the application object with const app = express(). app.get(path, handler) registers a GET route. If you also keep routes in an array, you can print how many routes exist at runtime. This project tests your Express + fs + middleware accumulation.
Route registration
You register a route with app.get(path, handler). The handler has the form (req, res) => res.json(data); req represents the request, res the response. res.json(data) sends the response as JSON.
Keeping routes in your own array lets you print the program's state. Example: routes.push({method: 'GET', path: '/health'}). Then console.log('Route count: ' + routes.length) prints a summary. Express's app.get registers the route; your array mirrors it. This way you can inspect the route list within the program.
Exercise Set up the Express skeleton
Import with const express = require('express'). Create the app with const app = express(). Define const routes = []. Add a health route with app.get('/health', (req, res) => res.json({ok: true})). Record it with routes.push({method: 'GET', path: '/health'}). Print console.log('Server ready').
Show example solution
const express = require('express');
const app = express();
const routes = [];
app.get('/health', (req, res) => res.json({ok: true}));
routes.push({method: 'GET', path: '/health'});
console.log('Server ready');
Test yourself
-
What does express() return?
const app = express();
- Bir dize
- A number
- Bir promise
- Uygulama nesnesi
Show answer
Uygulama nesnesi — express() returns the application object, used to add routes.
-
Import the express module
const express = require(____);
Show answer
const express = require('express');
-
Match each piece to its role
- express()
- Creates the app object
- app.get
- Registers a GET route
- res.json
- Sends a JSON response
- routes.push
- Adds the route to the array
Next lesson: Express API