First Steps
In this set of articles, you'll learn the core fundamentals of Nest. The main idea is to get familiar with essential Nest application building blocks. You'll build a basic CRUD application which features covers 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.
In the articles, we're mostly using TypeScript, but you can always switch the code snippets to the JavaScript version when it contains some TypeScript-specific expressions.
Prerequisites
Please make sure that Node.js (>= 6.11.0) is installed on your operating system.
Setup
Setting up a new project is quite simple with Starter repository. Just make sure that you have npm installed then use following commands in your OS terminal:
$ git clone https://github.com/nestjs/typescript-starter.git project
$ cd project
$ npm install
$ git clone https://github.com/nestjs/javascript-starter.git project
$ cd project
$ npm install
The
project directory will contain several core files inside
src directory.
Following the convention, newly created modules should have a 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 responsibility is to
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. The
create() method returns an object, which fulfills
INestApplication interface, and provides a set of usable methods, which are well described in the next chapters.
Running application
Once the installation process is completed, 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 main.ts file in the src directory.
While the application is running, open your browser and navigate to http://localhost:3000/. You should see the Hello world! message.