What is it like building a NodeJS API in 2022?

Anton Vroemans
CodeX
Published in
4 min readMar 31, 2022

For a school project, we had the opportunity to explore some new technologies. This gave us the chance to work with NodeJS for the first time and see what it has to offer.

NodeJS is an open source and multiplatform JavaScript-runtime-environment, with a built-in HTTP server. As the name suggests, it uses Javascript, as you probably already know it from many website front-ends. In recent years, it has gained tremendous popularity. This is also the case with serverless computing, which it can easily be used for.

When you say Node, you say NPM!

It is very minimalistic and therefore fast. There are over a million open-source packages that allow you to easily extend its functionality. The biggest challenge for me is to find a good software architecture within this scripting language. In this article I will take you through my findings and conclusions.

The Bartender app

This article will be mainly about our experience with NodeJS, but still we like to explain what we used it for and why we chose it. We had the opportunity to choose our own project and technologies. We came up with an idea to create an application in which consumptions could be easily tracked. Since we are all in associations, we are familiar with all the additional problems of keeping track of this correctly. With our app, we hope to change this.

We chose NodeJS for the back-end because we find it very interesting. Flutter was also a new technology. It allows you to write apps and websites. We would use this for the app on which consumptions can be indicated, and for a website on which everything can be managed. It is of course easy that both the app and the website can be written in the same language, but in practice the rather new Flutter web was not yet sufficiently suitable. Because of this we later decided to use the better known Angular framework for the website. This is also a new technology for us, but one that is much closer to the familiar web development.

Now back to NodeJS…

Router

Express is the go-to web framework for NodeJS. It contains all the most important features for a web server, but by far the most important, in my opinion, is the router.

You can associate middleware functions with certain paths. These are functions that are executed sequentially when a path is visited that meets a certain regex. This makes it easy to add some security or authentication middleware up front, for example. To keep the architecture simple and readable I wrote decorators that make it possible to assign a function to a certain path. Also for authentication I wrote a decorator that activates the appropriate middleware.

@Get('/club')
@RequiresAdmin()
async getClub(req: Request, res: Response) {
return res.status(200).json(req.club);
}

When you write your own decorator, it is really simply a function that inherits from the `MethodDecorator` type. The function is called with all parameters and metadata when the decorator is used somewhere. My routing decorators store all the parameters as additional metadata. Thus, when the application is started, all the info is retrieved and the routes are created manually.

Database

We’ve long since gotten out of the habit of writing our own database queries. An ORM (Object-relational mapping) has many advantages over writing queries yourself. It is easy to implement and fast (when used correctly). It also maintains a nice OO structure in the application.

Screenshot from https://npmjs.com/

TypeORM is an upcoming ORM package in typescript. It is actively maintained, and it doubled in popularity last year. So it fits perfectly into our project. Installing takes only 1 `npm install` command. When you modify the `ormconfig.json` file with the correct database data you can start immediately. We create models and repositories as regular classes, and everything works immediately. For example, to retrieve an item with a particular ID you do `await itemRepository.findOne(id)`. If we want to make a modification we modify the retrieved object, and update the database with `await itemRepository.save(item)`.

Conclusion

With the combination of simple routing and a database ORM, it is very easy to write an API. We also quickly found appropriate packages for other problems such as uploading and resizing images. We have a structured architecture in which everyone can find their way right away. Everything is simple and the project runs fast. In the future, we could further simplify the code by looking for additional validation or other packages.

All in all, we are very satisfied with NodeJS. We would like to use it more often in the future, especially for simple API applications.

--

--

CodeX
CodeX

Published in CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

Anton Vroemans
Anton Vroemans

Written by Anton Vroemans

I write mainly about security and programming. I look for effecient solutions to problems. Programming and electronics are my passion.

Responses (1)