Checkpoint · Lesson 1.3
Middleware, JSON bodies and errors: checkpoint files
Compare your folder with the reference using Git, if you cloned it: git diff lesson/m01-l03 -- src.
Files changed in this lesson
src/server.js changed
src/server.jsimport express from 'express';
const app = express();
const port = 3000;
app.use((req, res, next) => {
const started = Date.now();
res.on('finish', () => {
console.log(`${req.method} ${req.originalUrl} -> ${res.statusCode} (${Date.now() - started} ms)`);
});
next();
});
app.use(express.json());
const issues = [
{ id: 1, title: 'Lift stuck between floors 2 and 3', priority: 'high', status: 'open', building: 'Dublin Offices', occurredAt: '2026-11-02T09:15:00.000Z' },
{ id: 2, title: 'Dripping tap in the second-floor kitchen', priority: 'medium', status: 'closed', building: 'Dublin Offices', occurredAt: '2026-11-05T14:40:00.000Z' },
{ id: 3, title: 'Front door lock sticks at entrance 4', priority: 'critical', status: 'open', building: 'Cork Apartments', occurredAt: '2026-11-08T07:05:00.000Z' },
];
const PRIORITIES = ['low', 'medium', 'high', 'critical'];
function validateIssue(input) {
const errors = {};
const title = typeof input.title === 'string' ? input.title.trim() : '';
const building = typeof input.building === 'string' ? input.building.trim() : '';
if (title.length < 3 || title.length > 200) {
errors.title = 'Title must be between 3 and 200 characters.';
}
if (!PRIORITIES.includes(input.priority)) {
errors.priority = `Priority must be one of: ${PRIORITIES.join(', ')}.`;
}
if (building.length === 0) {
errors.building = 'Building is required.';
}
return { errors, values: { title, priority: input.priority, building } };
}
app.get('/', (req, res) => {
res.send('Caretaker is running');
});
app.get('/api/issues', (req, res) => {
const { status } = req.query;
const result = status ? issues.filter((issue) => issue.status === status) : issues;
res.json(result);
});
app.get('/api/issues/:id', (req, res) => {
const id = Number(req.params.id);
const issue = issues.find((e) => e.id === id);
if (!issue) {
return res.status(404).json({ error: 'Issue not found' });
}
res.json(issue);
});
let nextId = 4;
app.post('/api/issues', (req, res) => {
const { errors, values } = validateIssue(req.body ?? {});
if (Object.keys(errors).length > 0) {
return res.status(400).json({ errors });
}
const issue = { id: nextId++, ...values, status: 'open', occurredAt: new Date().toISOString() };
issues.push(issue);
res.status(201).location(`/api/issues/${issue.id}`).json(issue);
});
app.use((req, res) => {
res.status(404).json({ error: `No route for ${req.method} ${req.path}` });
});
app.use((err, req, res, next) => {
const status = err.status ?? 500;
if (status >= 500) {
console.error(err);
}
res.status(status).json({ error: err.expose ? err.message : 'Something went wrong' });
});
app.listen(port, () => {
console.log(`Caretaker listening on http://localhost:${port}`);
});
import express from 'express'; const app = express();const port = 3000; app.use((req, res, next) => { const started = Date.now(); res.on('finish', () => { console.log(`${req.method} ${req.originalUrl} -> ${res.statusCode} (${Date.now() - started} ms)`); }); next();}); app.use(express.json()); const issues = [ { id: 1, title: 'Lift stuck between floors 2 and 3', priority: 'high', status: 'open', building: 'Dublin Offices', occurredAt: '2026-11-02T09:15:00.000Z' }, { id: 2, title: 'Dripping tap in the second-floor kitchen', priority: 'medium', status: 'closed', building: 'Dublin Offices', occurredAt: '2026-11-05T14:40:00.000Z' }, { id: 3, title: 'Front door lock sticks at entrance 4', priority: 'critical', status: 'open', building: 'Cork Apartments', occurredAt: '2026-11-08T07:05:00.000Z' },]; const PRIORITIES = ['low', 'medium', 'high', 'critical']; function validateIssue(input) { const errors = {}; const title = typeof input.title === 'string' ? input.title.trim() : ''; const building = typeof input.building === 'string' ? input.building.trim() : ''; if (title.length < 3 || title.length > 200) { errors.title = 'Title must be between 3 and 200 characters.'; } if (!PRIORITIES.includes(input.priority)) { errors.priority = `Priority must be one of: ${PRIORITIES.join(', ')}.`; } if (building.length === 0) { errors.building = 'Building is required.'; } return { errors, values: { title, priority: input.priority, building } };} app.get('/', (req, res) => { res.send('Caretaker is running');}); app.get('/api/issues', (req, res) => { const { status } = req.query; const result = status ? issues.filter((issue) => issue.status === status) : issues; res.json(result);}); app.get('/api/issues/:id', (req, res) => { const id = Number(req.params.id); const issue = issues.find((e) => e.id === id); if (!issue) { return res.status(404).json({ error: 'Issue not found' }); } res.json(issue);}); let nextId = 4; app.post('/api/issues', (req, res) => { const { errors, values } = validateIssue(req.body ?? {}); if (Object.keys(errors).length > 0) { return res.status(400).json({ errors }); } const issue = { id: nextId++, ...values, status: 'open', occurredAt: new Date().toISOString() }; issues.push(issue); res.status(201).location(`/api/issues/${issue.id}`).json(issue);}); app.use((req, res) => { res.status(404).json({ error: `No route for ${req.method} ${req.path}` });}); app.use((err, req, res, next) => { const status = err.status ?? 500; if (status >= 500) { console.error(err); } res.status(status).json({ error: err.expose ? err.message : 'Something went wrong' });}); app.listen(port, () => { console.log(`Caretaker listening on http://localhost:${port}`);});Project tree at this checkpoint
.gitignore
.npmrc
package-lock.json
package.json
src/server.js