Getting Started with NestJS

We are getting started with NestJS. This document is a work in progress and it gets updated as we go. This document was updated to use NestJS 5.3.6 We use Visual Studio Code, download and install it. Open the console in VS Code with View|Integrated Terminal. Now create a new folder for the project on the console and open it as VS Code project folder:

 mkdir nestjs-backend code -r nestjs-backend

Install Node.js if you don’t have it yet. Check for npm version (currently 5.6.0), npm comes with Node.js.

npm -v

If the version is older than 5 update it with

npm install -g npm

Install TypeScript from https://www.typescriptlang.org/#download-links The following call may fail for Mac OSX and Linux users, it’s optional if you dont have admin rights. Leave it out. We will install TypeScript locally to the project later.

npm i -g typescript

Create our npm project file package.json, in folder nestjs-backend call:

npm init  -y

With -y nmp wont’t ask four additional input and uses the default values for the project. Open package.json and add in "scripts": { some more entries:

     "start": "node index.js",     "prestart:prod": "tsc",

Save the changes we made in package.json. In the console we add dependencies to NestJS:

 npm i -s @nestjs/common @nestjs/core @nestjs/microservices @nestjs/testing @nestjs/websockets reflect-metadata rxjs

We install TypeScript and type support for node and express.

 npm i --save-dev @types/node @types/express nodemon ts-node typescript

To create the file tsconfig.json, in folder nestjs-backend call:

tsc --init

Make sure that the latest TypeScript version is used. In Windows delete an old PATH entry. For Mac OSX and Linux users, use the version installed local to our project:

 ./node_module/typscript/bin/tsc --init

Open the generated file tsconfig.json and update it:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": false,
    "noImplicitAny": false,
    "removeComments": true,
    "noLib": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "sourceMap": true,
    "allowJs": true,
    "outDir": "./dist"
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ]
}

Create index.js in nestjs-backend

require('ts-node/register');
require('./src/main');

Create src folder in nestjs-backend

Create app.controller.ts in nestjs-backend/src

import { Get, Controller } from '@nestjs/common';

@Controller()
export class AppController {
	@Get()
	root(): string {
    return 'Hello World!';
  }
}

Create app.module.ts in nestjs-backend/src

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';

@Module({
  imports: [],
  controllers: [AppController],
  providers: [],
})
export class ApplicationModule {}

Create main.ts in nestjs-backend/src

import { NestFactory } from '@nestjs/core';
import { ApplicationModule } from './app.module';

async function bootstrap() {
	const app = await NestFactory.create(ApplicationModule);
	await app.listen(3000);
}
bootstrap();

We can start our NestJS app with:

npm start

Open a browser and go to http://localhost:3000

Install Visual Studio Code plugin: REST Client
https://marketplace.visualstudio.com/items?itemName=humao.rest-client

Create file rest-test.http

GET http://localhost:3000

###
http://localhost:3000/products

###
http://localhost:3000/products/product1

###
POST http://localhost:3000/products
Content-Type: application/json

{"id":1,"name":"product1","description":"First Product" }

Now we can use Visual Studio Code as a client for our service.

The ### is a separator for two requests.

# tslint

We don’t use tslint for our ‘Getting started with NestJS’ project. You can skip this part.

npm i -g tslint

create a basic configuration file tslint.json with:

tslint --init

update tslint.json

    "rules": {
        "quotemark": [
            true,
            "single"
        ],
        "member-access": [
            false
        ],
        "ordered-imports": [
            false
        ],
        "interface-name": [
            false
        ],
        "no-empty-interface": false,
        "no-empty": false,
        "arrow-parens": false,
        "object-literal-sort-keys": false
    }

I have put the source code at GitHub https://github.com/ptea/nestjs-backend

To continue the next part: Add TypeORM to NestJS Project

Leave a Reply

Your email address will not be published. Required fields are marked *