🚄Node On Rails: Finding the backend server: exploring NestJS Part 1!
Tags: javascript, typescript, beginners, webdev
Published: 2020-02-02 16:55:34


### Prolog: Thoughts on Javascript in the backend

On the fronted side of Javascript, we have these fantastic frameworks like vuejs, reactjs, or angular. They are all excellent technologies. Teams that don't have the size of Google or Facebook can now build Frontends that are on a level that you could not achieve some years ago.

The best practices for these are emerging. We now use Components to manage our frontends easier. Stores are used for better and more accessible data flow. Service workers help us with offline support. 

Looking at the backend, we also have several options. We have expressjs, fastify, MeteorJS, Sails.js, Koa.js, Hapi.js, derb.js, and so on. We have a lot of options. Some of these technologies are very barebones, and some are full-stack frameworks. Most of them are not opinionated. These frameworks want to cater to everyone. At least most of them. 

Looking for a more opinionated framework, usually, you will find non-javascript based frameworks like Laravel or Ruby On Rails. 

Folder Structure, generating code, build-in authentication, build-in authorization, security measures, code quality, and connections to data sources are standard features Software has today.

Even if you have different needs, you will need the before mentioned features. Finding best practices is not an easy task and leads to frustration. It sometimes feels like we are reinventing the wheel again and again.

Looking for a backend technology for "Node On Rails" is a more difficult task then I would like it to be.

I want to take you on the journey to finding a backend technology that fits. Talk with you about the good and bad parts.

We will start with nestjs today.

### Introduction to nestjs

From the nestjs website: 
> Nest (NestJS) is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with and fully supports TypeScript (yet still enables developers to code in pure JavaScript) and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).

Let's go through that text block step by step.

The first sentence is heathy on the buzzwords you want to here. It is an excellent hock. Tell me more.

The second sentence is lengthy. Let us split it up into two sections. Section one is from the start until the "and combines" section, and the second section is the rest.

Section one is significant to me. We can use Typescript, but we don't have too. Typescript is popular today. The only problem is to find developers that are experienced in Typescript compared to pure Javascript developers. Depending on the composition of your team, you can decide if you want to use Typescript or Javascript.

Section two, puh, this is a lot. OOP, FP, and FRP. OOP alone can be a hustle to learn, let alone all three of them. I'm also not sure if combining them is a good idea. I would instead like it to be only OOP or FP and FRP. Finding developers that are efficient and understand what to use when is way harder than finding someone that knows OOP or FP/FRP. I'm not sure if I like this kind of freedom. This can lead to spaghetti code. To a codebase where every `controller`, for example, is implemented differently. It is the hand of the development team to have a clear structure and way to implement parts of the app. This will lead to a discussion that could be avoided.

Next part of the introduction:
> Under the hood, Nest makes use of robust HTTP Server frameworks like Express (the default) and optionally can be configured to use Fastify as well!

> Nest provides a level of abstraction above these common Node.js frameworks (Express/Fastify) but also exposes their APIs directly to the developer. This allows developers the freedom to use the myriad of third-party modules that are available for the underlying platform.

Both of these sentences talk about the same thing. One is about the correct implementation (expressjs/fastify). The second one is about abstraction. In general light, abstraction is a good thing. Right now, expressjs is the most popular framework, but this can change in 2020 or 2021. Changing the HTTP server under the hood in an easy and fast was is always a plus for me.

### Philosophy of nestjs

Okay next topic is the 

> Nest provides an out-of-the-box application architecture that allows developers and teams to create highly testable, scalable, loosely coupled, and easily maintainable applications.

Nice! Everything I want! Sadly this is all about we hear about architecture here. Pressing CTRL+F and searching for "Architecture" or using the search shows zero hits. If someone from nestjs reads this: Please add this chapter/page to the documentation. If you need help. Just message me on any platform. I'm more than glad to help.

Looking at the rest of the claims. Testable is a must-have for me. I will not work with frameworks that are not easy to write tests for. Having worked on big projects that need to maintainable. Tests are something you need, and there should be no discussion about this.

Usually, loosely coupled code is also code that scales. A Framework can help to make code more maintainable. More often, maintainable code is a team effort than a Framework effort.

### Installing nestjs

> To get started, you can use either scaffold the project with the Nest CLI, or clone a starter project (both will produce the same outcome).

From the sentence above, it looks like the CLI does a clone of a git repo. 

The rest of this section talks about how to set up a new project. I will do that in my own words. 

You have two possibilities. Using the nestjs CLI tool or just cloning one of two git repos.
You can install the nestjs CLI with npm or yarn like that:
```bash
sudo npm i -g @nestjs/cli
#or
yarn global add @nestjs/cli
```
Now you should have the `nest` command available.
```bash
nest --version
```
to create a new project with Typescript and npm, you can run the following command:
```bash
nest new MyProjectName
```

If you want to use Javascript or Yarn, you can add arguments to the `new` command.

```bash
nest new MyProjectName --language JavaScript --package-manager Yarn
```
You can mix and match the arguments as you want them to be!

The second way is to clone one of the two following repos.

The typescript repo:
```bash
 git clone https://github.com/nestjs/typescript-starter.git MyProject
```

The Javascript repo:
```bash
git clone https://github.com/nestjs/javascript-starter.git MyProject
```

Then you need to run the following command to install all dependencies and to start the server:
```bash
cd MyProject
npm install # or `yarn`
npm run start # or yarn start
```
Now you have a development server running under [http://localhost:3000](http://localhost:3000).

If you want to read all of this without my comments, you can find it here: [https://docs.nestjs.com/](https://docs.nestjs.com/)


That is all for this week! Next week we will continue with the exploration of nestjs! So consider smashing the follow button.

If you liked this content, please click the heart or the unicorn!

If you want to read it later, click the bookmark button under the unicorn!

**👋Say Hello!** [Instagram](https://www.instagram.com/lampewebdev/) | [Twitter](https://twitter.com/lampewebdev) | [LinkedIn](https://www.linkedin.com/in/michael-lazarski-25725a87) | [Medium](https://medium.com/@lampewebdevelopment) | [Twitch](https://dev.to/twitch_live_streams/lampewebdev) | [YouTube](https://www.youtube.com/channel/UCYCe4Cnracnq91J0CgoyKAQ)