Book Image

Nest.js: A Progressive Node.js Framework

By : Greg Magolan, Patrick Housley, Adrien de Peretti, Jay Bell, David Guijarro
Book Image

Nest.js: A Progressive Node.js Framework

By: Greg Magolan, Patrick Housley, Adrien de Peretti, Jay Bell, David Guijarro

Overview of this book

Nest.js is a modern web framework built on a Node.js Express server. With the knowledge of how to use this framework, you can give your applications an organized codebase and a well-defined structure. The book begins by showing how to use Nest.js controllers, providers, modules, bootstrapping, and middleware in your applications. You’ll learn to use the authentication feature of Node.js to manage the restriction access in your application, and how to leverage the Dependency Injection pattern to speed up your application development. As you advance through the book, you'll also see how Nest.js uses TypeORM—an Object Relational Mapping (ORM) that works with several relational databases. You’ll use Nest.js microservices to extract part of your application’s business logic and execute it within a separate Nest.js context. Toward the end of the book, you’ll learn to write tests (both unit tests as well as end-to-end ones) and how to check the percentage of the code your tests cover. By the end of this book, you’ll have all the knowledge you need to build your own Nest.js applications.
Table of Contents (16 chapters)

Serving the Angular Universal App with Nest.js

Now that we are going to be serving the Angular app with our Nest.js server, we are going to have to compile them together so that when our Nest.js server is run, it knows where to look for the Universal app. In our server/src/main.ts file there are a couple of key things we need to have in there. Here we create a function bootstrap() and then call it from below.

async function bootstrap() {
  if (environment.production) {
    enableProdMode();
  }

  const app = await NestFactory.create(ApplicationModule.moduleFactory());

  if (module.hot) {
    module.hot.accept();
    module.hot.dispose(() => app.close());
  }

  await app.listen(environment.port);
}

bootstrap()
  .then(() => console.log(`Server started on port ${environment.port}`))
  .catch(err => console.error(`Server startup failed`, err));

Let’s step through this function line by line.

if (environment.production) {
    enableProdMode();
  }

This tells the application...