Javascript

Build modern & scalable Node.js web applications with Nest

JavaScript is awesome. Node.js gave us a possibility to use this language also on the server side. There are a lot of amazing libraries, helpers and tools on this platform, but non of them do not solve the main problem – the architecture. This is why I decided to create Nest framework.

This article is outdated. Take a look here.

Why Nest?

Nest is a powerful web framework for Node.js, which allows you to easily build efficient, scalable applications. It uses modern JavaScript, is built with TypeScript and bring best concepts to JavaScript back-end world such as Dependency Injection.

It is not just another framework. You do not have to wait on large community, because Nest is built with awesome, popular well-known libraries – Express and socket.io! It means, that you could quickly start using framework with no worries about a third party plugins.

Nest is inspired by Spring and Angular

Modular

With Nest you can naturally split your code into separated and reusable modules. Nest Module is a class with @Module() decorator. This decorator provides metadata, which framework uses to organize application structure.


@Module({
    modules: [ SharedModule ],
    controllers: [ UsersController ],
    components: [ UsersService ],
    exports: [ UsersService ],
})
export class UsersModule {}

By default, modules encapsulate each dependency. Read more about modules here.

Components as a building blocks

Almost everything is a component – Service, Repository, Provider etc. and they might be injected to controllers or to another component by constructor (as in Angular). Component is a plain class with @Component() decorator.


@Component()
export class UsersService {}

Read more about components here.

Handling HTTP requests – Controllers layer

The Controllers layer is responsible for handling incoming HTTP requests. In Nest, Controller is a simple class with @Controller() decorator. Example:


@Controller({ path: 'users' })
export class UsersController {
    constructor(private usersService: UsersService) {}

    @RequestMapping()
    async getAllUsers(req, res) {
        const users = await this.usersService.getAllUsers();
        res.status(200).json(users);
    }

    @RequestMapping({ path: '/:id' })
    async getUser(req, res) {
        const user = await this.usersService.getUser(req.params.id);
        res.status(200).json(user);
    }

    @RequestMapping({ method: RequestMethod.POST })
    async addUser(req, res) {
        const msg = await this.usersService.getUser(req.body.user);
        res.status(201).json(msg);
    }
}

As you can see, methods in Nest controllers has the same list of arguments and behaviour as a simple routes in Express.

In fact, Nest Controller methods are some kind of encapsulated Express routes. Read more about controllers here.

Real-time applications support

There are special components in Nest called Gateways. Gateways helps us to create real-time web apps. They are some kind of encapsulated socket.io features adjusted to framework architecture.


@SocketGateway({ port: 81, namespace: 'users' })
export class UsersGateway implements NestGateway {
    @GatewayServer server;

    afterInit(server) {}
    handleConnection(client) {}
    handleDisconnect(client) {}

    @SubscribeMessage({ value: 'drop' })
    onDropMessage(sender, data) {
        // sender is a native socket.io client object
    }
}

Read more about gateways here

Testing

Nest gives you a set of test utilities, which boost application testing process. There are two different approaches to test your components and controllers – isolated tests or with dedicated Nest test utilities.

Those Test Utilities are placed in static Test class (nest.js/testing module). This class provide two methods:

  • createTestingModule(metadata: ModuleMetadata), which receives as an parameter simple module metadata. This method creates a Test Module (the same as in real Nest Application) and stores it in memory.
  • get<T>(metatype: Metatype<T>), which returns instance of chosen (metatype passed as parameter) controller / component (or null if it is not a part of module).

Test.createTestingModule({
    controllers: [ UsersController ],
    components: [ UsersService ]
});
const usersService = Test.get(UsersService);

Read more about testing here

Middlewares, error handling and more…

There are plenty of another awesome Nest features. You can read about them in Documentation & Overview. If you are looking for starter repos and more specific details about Nest, take a look at GitHub repository. Let’s bring modern concepts to Node.js together!