First steps
In this set of articles, you'll learn the core fundamentals of Nest. To get familiar with the essential building-blocks of Nest applications, we'll build a basic CRUD application with features that cover a lot of ground at an introductory level.
Language
We're in love with TypeScript, but above all - we love Node.js. That's why Nest is compatible with both TypeScript and pure JavaScript. Nest is taking advantage of latest language features, so to use a framework with simple JavaScript we need a Babel compiler.
We'll mostly use TypeScript in the examples we provide, but you can always switch the code snippets to vanilla JavaScript syntax.
Prerequisites
Please make sure that Node.js(>= 8.9.0) is installed on your operating system.
Setup
Setting up a new project is quite simple with the Nest CLI. With npm installed, you can create a new Nest project with the following commands in your OS terminal:
$ npm i -g @nestjs/cli
$ nest new projectThe project directory will be scaffolded with several core files being located within a src/ directory.
Following the convention, newly created modules should have their dedicated directory.
main.ts | The entry file of the application. It uses NestFactory to create the Nest application instance. |
app.module.ts | Defines AppModule, the root module of the application. |
app.controller.ts | Basic controller sample with a single route. |
The main.ts includes an async function, which will bootstrap our application:
import { NestFactory } from '@nestjs/core';
import { ApplicationModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(ApplicationModule);
await app.listen(3000);
}
bootstrap();
To create a Nest application instance, we are using the NestFactory. NestFactory is one of the most fundamental classes, it exposes a few static methods that allows creating application instance. The create() method returns an object, which fulfills the INestApplication interface, and provides a set of usable methods which are well described in the next chapters.
Running application
Once the installation process is complete, you can run the following command to start the HTTP server:
$ npm run start This command starts the HTTP server on the port defined inside the src/main.ts file. While the application is running, open your browser and navigate to http://localhost:3000/. You should see the Hello world! message.
