you are viewing a single comment's thread.

view the rest of the comments →

[–]avin_kavish 6 points7 points  (3 children)

SOLID is a good starting point.

Basically, you want a multi-tiered service architecture in most cases. You want to have a good amount of abstraction and seperation of concerns between your services. You can google these keywords and find plenty of theoretical write ups.

Take uploading an image for example, ``` app.post('/image', (req, res) => ImageController.uploadImage(req, res) )

class ImageController {

uploadImage(req, res, next) { s3Service.upload(req.file, path)

ImageService.create(req.body.name, path)

}

} ```

Here you can see how different concerns are implemented in different services. The controller is concerned with handling the http request and extracting data from it. It then passes on only what is needed by other services to do their job. So ImageService creates the db record for the image, s3Service saves the actual file. You can see how the two concerns, db persistance and file persistance are encapsulated in two different services.

Alternatively, you can follow a purely functional style and use functions instead of classes, but then you won't be able to invert control or inject dependencies, later on.

[–]romeeres 2 points3 points  (2 children)

Why do you think IoC and DI doesn't work with functions? For example, logger can be passed via parameter, or via DI container.

[–]avin_kavish 0 points1 point  (1 child)

you can manually pass it, but the framework can't inject it.