Recipe of the Day
Part of the Express Basics section of Coddy's Node.js journey. Lesson 19 of 20.
Recipe Box gets a recipe of the day: the same recipe for everyone all day, a different one tomorrow. The date picks it, and ?date= lets you try any day.
new Date('2026-03-01') is midnight UTC on that day. Its day of the year counts from 31 December, so 1 January is day 1: (date - Date.UTC(year, 0, 0)) / 86400000, since a day has 86,400,000 milliseconds. A text that is no date gives an invalid date, whose getTime() is NaN.
Add the route in server.js, above the 404 handler. Pick from the recipes in data/recipes.json, like the API does, so a recipe someone posts can be the recipe of the day: export loadRecipes from routes/recipes.js and await it.
Challenge
EasyGET /api/today?date=2026-03-01answersrecipes[day % recipes.length]as JSON,recipescoming fromloadRecipes()anddaybeing the day of the year.- No
date: today. - A date that does not parse:
400with{ "error": "Use a date like 2026-03-01" }.
Send ?date=2026-03-01 twice, then ?date=soon.
Try it yourself
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'));
const escapeHtml = (text) =>
text
.replaceAll('&', '&')
.replaceAll('<', '<')
.replaceAll('>', '>')
.replaceAll('"', '"');
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');
});