CodeBlanks Logo Icon CodeBlanks

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.

Practice 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').

Starter code

index.js
// Import the express module and create the app
// Add the /health route, track routes in an array
// Print 'Server ready'
// write here

Sample solution

index.js
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');

Expected output

Sunucu hazır

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does express() return?

index.js
const app = express();
  • Bir dize
  • A number
  • Bir promise
  • Uygulama nesnesi Correct answer

Explanation: express() returns the application object, used to add routes.

2 Import the express module

Question
const express = require(____);
Answer
const express = require('express');

Explanation: The express module is loaded with require('express').

3 Match each piece to its role

Term Meaning
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

All lessons in Intermediate