Friday 26 June 2020

Which packages can replace the hapi framework that is scheduled for deprecation?

I had just stared working on a new API when I learned that hapi is scheduled for deprecation.Migrating to hapi from express has been a blessing for simpler projects because authentication and validation (using joi) was natively and, more importantly, elegantly supported. Same applies to unit testing using lab and code.I’ve spent a few hours trying to put together an equivalent stack "à la carte" on express and things don’t feel right. FYI, I am using TypeScript.Currently, I am considering using express, express-validator, passport and supertest as alternatives... Is this a descent replacement stack? I am a little disoriented.I have no problem with express, but the semantics of express-validator isn’t as clean as joi.```javascript // ...rest of the initial code omitted for simplicity. const { body, validationResult } = require('express-validator');app.post('/user', [ // username must be an email body('username').isEmail(), // password must be at least 5 chars long body('password').isLength({ min: 5 }) ], (req, res) => { // Finds the validation errors in this request and wraps them in an object with handy functions const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(422).json({ errors: errors.array() }); }User.create({ username: req.body.username, password: req.body.password }).then(user => res.json(user)); }); ```This is the part I dislike the most... I would like to abstract this logic once instead of adding it to all handlers.javascript // Finds the validation errors in this request and wraps them in an object with handy functions const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(422).json({ errors: errors.array() }); }Ideas? How are you guys handling this on Express? And what stack are you using or considering using to replace hapi?Sending my love to Eran for outstanding (and certainly exhausting) work.

Submitted June 26, 2020 at 03:17PM by sunknudsen

No comments:

Post a Comment