This lesson adds final touches to the app’s setup and resilience. After introducing EJS in the previous lesson, it focuses on simplifying how the application is started, improving how errors are surfaced to both developers and users, and enabling Express to serve static assets and standalone error pages. The overall goal is to make development smoother and user experiences clearer when things go wrong.
First, the package.json scripts section is updated to include a start command so the app can be launched with npm start, abstracting away the main file name. The lesson then introduces structured error handling with Express by adding a dedicated error controller. It demonstrates a four-parameter middleware to log error stacks and pass them along, and implements catch-all handlers for missing routes (404) and server errors (500), using http-status-codes for clarity. Placement matters: these handlers are registered after normal routes so they act as fallbacks, and they can return either plain text or custom error pages.
Finally, the app is configured to serve static files from a public directory using express.static. This enables direct delivery of assets like images and HTML files without a templating engine—crucial for reliable 404.html and 500.html pages that should render even if view rendering fails. With logging, custom error responses, and static file serving in place, the application becomes easier to run, debug, and present professionally to end users.
FAQ
What does the scripts object in package.json do?It lets you define command aliases you can run with npm (for example, a start script), so you can execute common tasks with simple commands like npm start.How do I set up npm start for my app?Add "start": "node main.js" under scripts in package.json. Then start your app with npm start (or npm run start / npm run-script start).Where should error-handling middleware go in Express?After all your regular routes and middleware. Order matters: put 404 and 500 handlers at the end so they act as fallbacks.How do I write an Express error-logging middleware?Create a middleware with four parameters (error, req, res, next), log error.stack (for example, with console.error), then call next(error) to pass it along.How can I return custom 404 and 500 responses?Add controller functions that set the status code (for example, NOT_FOUND or INTERNAL_SERVER_ERROR) and send a message or serve a static HTML file (404.html / 500.html).What is the http-status-codes package used for?It provides readable constants (like NOT_FOUND and INTERNAL_SERVER_ERROR) so you don’t hardcode numbers like 404 or 500.How do I serve static files (like 404.html) with Express?Enable static hosting with app.use(express.static('public')). Place files in the public folder and access them by URL (for example, /404.html or /images/cat.jpg).Why do I see “Cannot GET /” in the browser?There’s no route matching the requested path. Add a route for that path or let your 404 handler catch it.How can I test that my error handler is working?Intentionally trigger an error in a route (for example, reference an undefined variable), visit that route, and confirm the stack is logged and your 500 response is sent.What if npm start doesn’t work after I add the script?Verify package.json syntax (commas, quotes), confirm the main file name/path, and try running node main.js directly to isolate the issue.
pro $24.99 per month
access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!