Recipe of the Day
Part of the Express Basics section of Coddy's Node.js journey. Lesson 22 of 23.
Recipe Box can use familiar route and file-reading skills to serve one recipe for a day. The supplied dayOfYear helper turns a date into a number: 1 January is 1 and 1 March 2026 is 60. A missing date means today; unreadable text returns null.
const day = dayOfYear(req.query.date);
// ?date=2026-03-01 gives 60
// ?date=soon gives nullUse the remainder operator to choose a position: day % recipes.length. With four recipes, day 60 selects position zero. Load the recipes inside the request so recently posted recipes take part too. Date parsing is already supplied; focus on composing the route.
Challenge
EasyAdd a Recipe of the Day endpoint to Recipe Box. The supplied day-of-year.js exports dayOfYear(text), returning the day number or null for an invalid date.
- Export
loadRecipesfromroutes/recipes.js. Import it intoserver.js: the router remains the default import, and{ loadRecipes }follows it after a comma. - Import
dayOfYear. Inserver.js, above the 404 handler, add anasyncroute forGET /api/today. Passreq.query.dateto the helper. - For
null, return 400 with{"error":"Use a date like 2026-03-01"}. - Otherwise, await
loadRecipes()and returnrecipes[day % recipes.length]as JSON. Keep the existing routes and error handler.
Try GET /api/today?date=2026-03-01: it returns the first recipe because the day number is 60 and there are four recipes. Try ?date=soon: it returns the 400 error above.
Try it yourself
import { escapeHtml } from './html.js';
import express from 'express';
import { randomUUID } from 'node:crypto';
import { recipes } from './data.js';
import recipesRouter from './routes/recipes.js';
import reviewsRouter from './routes/reviews.js';
const app = express();
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`);
next();
});
app.use((req, res, next) => {
res.set('X-Request-Id', randomUUID());
next();
});
app.use(express.json());
app.use(express.static('public'));
app.get('/', (req, res) => {
const items = recipes.map((r) => `<li><a href="/recipes/${r.id}">${escapeHtml(r.title)}</a></li>`);
res.send(`<link rel="stylesheet" href="/style.css"><h1>Recipe Box</h1><ul>${items.join('')}</ul>`);
});
// type('text') sends plain text, so each title shows on its own line.
app.get('/recipes', (req, res) => {
const { tag } = req.query;
const list = tag ? recipes.filter((recipe) => recipe.tags.includes(tag)) : recipes;
res.type('text').send(list.map((recipe) => recipe.title).join('\n'));
});
app.get('/recipes/:id', (req, res) => {
const recipe = recipes.find((r) => r.id === Number(req.params.id));
if (!recipe) {
res.status(404).send('No such recipe');
return;
}
res.send(recipe.title);
});
app.use('/api/recipes', recipesRouter);
app.use('/api/recipes/:id/reviews', reviewsRouter);
app.get('/about', (req, res) => {
res.send('Recipe Box collects family recipes.');
});
app.get('/menu', (req, res) => {
res.redirect(301, '/recipes');
});
app.get('/menu/:id', (req, res) => {
res.redirect(301, `/recipes/${req.params.id}`);
});
app.use((req, res) => {
if (req.path.startsWith('/api/')) {
res.status(404).json({ error: 'Not found' });
return;
}
res.status(404).send('<h1>Page not found</h1><p><a href="/">Back to Recipe Box</a></p>');
});
app.use((err, req, res, next) => {
console.error(err);
res.status(500).json({ error: 'Something went wrong' });
});
app.listen(3000, () => {
console.log('Recipe Box is running on port 3000');
});
All lessons in Express Basics
Practice on your own: Express playground