Functional Design Patterns for Express.js by Jonathan Lee Martin
Author:Jonathan Lee Martin
Language: eng
Format: epub
Published: 2019-06-11T16:23:28+00:00
Route Middleware
Sadly not all is well. Try sending a GET /emails request with Insomnia. The request seems to be hanging again because an exception is blowing everything up:
GET /emails (node:44439) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at jsonBodyParser (lib/json-body-parser.js:5:19) [···]
Whatâs going on? Well, not every route expects a request body, much less a JSON-formatted body. But jsonBodyParser() runs before every single route as though a JSON-formatted request body is guaranteed. GET requests donât have a body, so JSON.parse() is trying to parse an empty string.
There are a few approaches to fix this bug. The typical solution is to make jsonBodyParser() a bit more robust to edge cases with some if...else statements. However, apart from making our code uglier, it only postpones other bugs that will emerge because it wonât solve the underlying design problem: only two routes in our backend expect JSON-formatted request bodies!
Inserting middleware with app.use() is a bit like using global variables: tempting and easy, but deadly to reusable software. With few exceptions, âglobal middlewareâ is a bad design choice because it is more difficult to âopt-outâ of middleware in a few routes than it is to âopt-inâ where itâs needed.
Figure 5.3: Route middleware is like a personalized stack for just this route. Instead of adding global middleware with app.use(), we can specify middleware for individual routes with .get() and its siblings. Letâs try it in routes/emails.js:
routes/emails.js const express = require('express'); + const jsonBodyParser = require('../lib/json-body-parser'); const generateId = require('../lib/generate-id'); const emails = require('../fixtures/emails'); [···] emailsRouter.route('/') .get(getEmailsRoute) - .post(createEmailRoute) + .post(jsonBodyParser, createEmailRoute) ; emailsRouter.route('/:id') .get(getEmailRoute) - .patch(updateEmailRoute) + .patch(jsonBodyParser, updateEmailRoute) .delete(deleteEmailRoute) ; [···]
You can add as many middleware functions for a route as you want. They will execute from left to right, so make sure your route function comes last. To get our code working again, letâs nuke jsonBodyParser() from the global middleware stack in index.js:
index.js const express = require('express'); const logger = require('./lib/logger'); - const jsonBodyParser = require('./lib/json-body-parser'); [···] app.use(logger); - app.use(jsonBodyParser); app.use('/users', usersRouter); app.use('/emails', emailsRouter); [···]
Try a few requests again in Insomnia, like POST /emails, PATCH /emails/1 and GET /emails. They should all be working as before!
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Windows Terminal Tips, Tricks, and Productivity Hacks by Will Fuqua(1522)
Augmented Reality with Unity AR Foundation by Jonathan Linowes(1274)
Sketching User Experiences by Buxton Bill(640)
Object-Role Modeling Fundamentals: A Practical Guide to Data Modeling with ORM by Terry Halpin(614)
Beginning AutoCAD 2020 Exercise Workbook by Cheryl R. Shrock(578)
You Donât Know JS: Up & Going by Kyle Simpson(561)
SketchUp For Dummies by Bill Fane & Mark Harrison & Josh Reilly(527)
Adobe InDesign for Absolute Beginner: Tips and Techniques to Creating Professional Books and Magazines for Online and Desktop Publishing using InDesign by Scott Walter & Scott Walter(527)
Windows 10: The Missing Manual by Pogue David(522)
AI 2041: Ten Visions for Our Future by Kai-Fu Lee & Chen Qiufan(504)
100 Principles of Game Design by unknow(491)
Advanced Game Design by Michael Sellers(485)
Research Methods in Human-Computer Interaction by unknow(478)
TensorFlow 2.0 Computer Vision Cookbook by Jesus Martinez(471)
The Rainbow Stack: A Casual Guide to UX Design by Jawara Joseph(469)
Photoshop Elements 12 All-in-One For Dummies by Barbara Obermeier & Ted Padova(453)
Mastering InDesign Templates by Murphy Monica & Knorr Mancini Margot(452)
Hands-On Data Visualization by Jack Dougherty(452)
Connected, Intelligent, Automated: The Definitive Guide to Digital Transformation and Quality 4.0 by Radziwill N. M(423)
