Friday 28 July 2017

Naming conventions in Express: middleware vs. controllers

I have a question about naming conventions in the NodeJS/Express ecosystem. I understand that middleware is just a function that takes a request/response object can manpiulate those objects, end or continue the response cycle, etc. I also understand that routing controllers are used to define route actions. I am confused when a method would be considered "middleware" vs when it would be considered a "controller". If we imagine a scenario where a user uploads a photo, then a server manipulates the photo (e.g. adds a watermark), uploads the photo to s3, and then saves a Photo model instance in our database, we might have something like the following:function manipulatePhoto(req, res, next) { next() } function uploadPhoto(req, res, next) { // upload photo to s3 next() } function createPhoto(req, res) { // create new photo using ORM then send response PhotoModel.create({}) .then(() => { res.status(201).send(photos); }); } router.post('/photos/', manipulatePhoto, uploadPhoto, createPhoto); In the example above, would each method be considered a middleware method? Would all the methods fall under the umbrella of a "PhotosController"?

Submitted July 28, 2017 at 04:23PM by mrc1897

No comments:

Post a Comment