{"id":6837,"date":"2020-09-07T07:29:03","date_gmt":"2020-09-07T05:29:03","guid":{"rendered":"https:\/\/blog.via-internet.de\/?p=6837"},"modified":"2020-09-07T07:29:03","modified_gmt":"2020-09-07T05:29:03","slug":"nestjs-getting-started-part-1","status":"publish","type":"post","link":"https:\/\/via-internet.de\/blog\/2020\/09\/07\/nestjs-getting-started-part-1\/","title":{"rendered":"NestJS | Getting started &#8211; Part 1"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a rel=\"noreferrer noopener\" href=\"https:\/\/nestjs.com\/\" target=\"_blank\">NestJS<\/a>&nbsp;(just Nest from here on out), is a Node framework meant to build server-side applications. Not only is it a framework, but it is also a platform to meet many backend application needs, like writing APIs, building microservices, or doing real-time communications through web sockets.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Nest is also heavily influenced by Angular, and you will immediately find its concepts familiar. The creators of Nest strived to make the learning curve as small as possible, while still taking advantage of many higher level concepts such as modules, controllers, and dependency injection.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Install NodeJS<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Download NodeJS from <a rel=\"noreferrer noopener\" href=\"https:\/\/nodejs.org\/en\/download\/\" target=\"_blank\">here<\/a> and install as described <a rel=\"noreferrer noopener\" href=\"https:\/\/nodejs.org\/en\/download\/package-manager\/\" target=\"_blank\">here<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, on macOS using Homebrew<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">brew install node<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Or download the package<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">curl \"https:\/\/nodejs.org\/dist\/latest\/node-${VERSION:-$(wget -qO- https:\/\/nodejs.org\/dist\/latest\/ | sed -nE 's|.*>node-(.*)\\.pkg&lt;\/a>.*|\\1|p')}.pkg\" > \"$HOME\/Downloads\/node-latest.pkg\" &amp;&amp; sudo installer -store -pkg \"$HOME\/Downloads\/node-latest.pkg\" -target \"\/\"<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Install NextJS<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npm i -g @nestjs\/cli<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Create server App<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Create new server App<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">nest new demo.server<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Start server App<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cd demo.server<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npm run start:dev<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now open browser on <a href=\"http:localhost:3000\" target=\"_blank\" rel=\"noreferrer noopener\">http: localhost:3000<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">App Structure<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">main.ts<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import { NestFactory } from '@nestjs\/core';\nimport { AppModule } from '.\/app.module';\n\nasync function bootstrap() {\n  const app = await NestFactory.create(AppModule);\n  await app.listen(3000);\n}\nbootstrap();<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">app.module.ts<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import { Module } from '@nestjs\/common';\nimport { AppController } from '.\/app.controller';\nimport { AppService } from '.\/app.service';\n\n@Module({\n  imports: [],\n  controllers: [AppController],\n  providers: [AppService],\n})\nexport class AppModule {}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">app.controller.ts<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import { Controller, Get } from '@nestjs\/common';\nimport { AppService } from '.\/app.service';\n\n@Controller()\nexport class AppController {\n  constructor(private readonly appService: AppService) {}\n\n  @Get()\n  getHello(): string {\n    return this.appService.getHello();\n  }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">app.service.ts<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import { Injectable } from '@nestjs\/common';\n\n@Injectable()\nexport class AppService {\n  getHello(): string {\n    return 'Hello World!';\n  }\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Add Functionality<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Create service and controller<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">nest g service missions\nnest g controller missions<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Modify <code>mission service<\/code><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Injectable()\nexport class MissionsService {\n  missions: Mission[] = [\n    { id: 1, title: 'Rescue cat stuck in asteroid', reward: 500, active: true, },\n    { id: 2, title: 'Escort Royal Fleet', reward: 5000, active: true, },\n    { id: 3, title: 'Pirates attacking the station', reward: 2500, active: false, },\n  ];\n\n  async getMissions(): Promise&lt;Mission[]> {\n    return this.missions;\n  }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Modify <code>mission controller<\/code><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Controller('missions')\nexport class MissionsController {\n  constructor(private missionsService: MissionsService) {}\n\n  @Get()\n  getMissions() {\n    return this.missionsService.getMissions();\n  }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Open in browser: <a href=\"http:\/\/localhost:3000\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/localhost:3000<\/a><\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/blog.via-internet.de\/wp-content\/uploads\/2020\/08\/Bildschirmfoto-2020-08-31-um-17.04.10-700x509.png\" alt=\"\" class=\"wp-image-6845\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Create Frontend App<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Create new frontend App<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ionic start demo.frontend sidemenu<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Working with Database and TypeORM<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Create \/ Sync database with schema<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add command to package.json<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ini\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\"scripts\": {\n    \"typeorm\": \"ts-node -r tsconfig-paths\/register .\/node_modules\/typeorm\/cli.js\"\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npm run typeorm schema:sync<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">TypeORM Commands<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">schema:sync         \nSynchronizes your entities with database schema. It runs schema update queries on all connections you have. To run update queries on a concrete connection use -c option.\nschema:log          \nShows sql to be executed by schema:sync command. It shows sql log only for your default connection. To run update queries on a concrete connection use -c option.\nschema:drop         \nDrops all tables in the database on your default connection. To drop table of a concrete connection's database use -c option.\nquery               \nExecutes given SQL query on a default connection. Specify connection name to run query on a specific connection.\nentity:create       \nGenerates a new entity.\nsubscriber:create   \nGenerates a new subscriber.\nmigration:create    \nCreates a new migration file. [Aliase: migrations:create]\nmigration:generate  Generates a new migration file with sql needs to be executed to update schema. [Aliase: migrations:generate]\nmigration:run       \nRuns all pending migrations. [Aliase: migrations:run]\nmigration:show      \nShow all migrations and whether they have been run or not\nmigration:revert    \nReverts last executed migration. [Aliase: migrations:revert]\nversion             \nPrints TypeORM version this project uses.\ncache:clear         \nClears all data stored in query runner cache.\ninit                \nGenerates initial TypeORM project structure. If name specified then creates files inside directory called as name. If its not specified then creates files inside current directory.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Additional readings<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/ionicframework.com\/blog\/full-stack-typescript-with-ionic-angular-and-nestjs-part-1\/\"><\/a><a href=\"https:\/\/ionicframework.com\/blog\/full-stack-typescript-with-ionic-angular-and-nestjs-part-1\/\">Full-Stack TypeScript with Ionic, Angular, and NestJS Part 1<\/a><\/li><li><a href=\"https:\/\/www.techiediaries.com\/nestjs-tutorial-rest-api-crud\/\">Nest.js Tutorial: Build your First REST API CRUD App with TypeORM\/<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/whatthecode.dev\/nestjs-typeorm-pagination-step-by-step-guide\/\" target=\"_blank\">NestJS TypeORM Pagination \u2013 Step By Step Guide\/<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.theodo.com\/2019\/05\/an-overview-of-nestjs-typeorm-release-your-first-application-in-less-than-30-minutes\/\" target=\"_blank\">Getting started with NestJS &amp; TypeORM (+bonus NestJS Admin)<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/codersera.com\/blog\/typeorm-with-nest-js-tutorial\/\" target=\"_blank\">TypeORM With NEST JS Basic Tutorial<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/eismaenners.de\/2019\/12\/13\/learning-nestjs\/\" target=\"_blank\">Learning nestjs I<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/medium.com\/@gausmann.simon\/nestjs-typeorm-and-postgresql-full-example-development-and-project-setup-working-with-database-c1a2b1b11b8f\" data-type=\"URL\" data-id=\"https:\/\/medium.com\/@gausmann.simon\/nestjs-typeorm-and-postgresql-full-example-development-and-project-setup-working-with-database-c1a2b1b11b8f\" target=\"_blank\">NestJS, TypeORM and PostgreSQL \u2014 full example development and project setup working with database migrations.<\/a><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction NestJS&nbsp;(just Nest from here on out), is a Node framework meant to build server-side applications. Not only is it a framework, but it is also a platform to meet many backend application needs, like writing APIs, building microservices, or doing real-time communications through web sockets. Nest is also heavily influenced by Angular, and you will immediately find its concepts familiar. The creators of Nest strived to make the learning curve as small as possible, while still taking advantage of many higher level concepts such as modules, controllers, and dependency injection. Installation Install NodeJS Download NodeJS from here and install as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6851,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[91,98,51],"tags":[],"class_list":["post-6837","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angularjs","category-ionic","category-nestjs"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/6837","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/comments?post=6837"}],"version-history":[{"count":0,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/6837\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/media?parent=6837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/categories?post=6837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/tags?post=6837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}