Skip to content

Commit 789ccce

Browse files
nest intro
1 parent 0ac7b9f commit 789ccce

1 file changed

Lines changed: 156 additions & 0 deletions

File tree

README.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,3 +760,159 @@ It uses package @nestjs/microservices.
760760
```https://docs.nestjs.com/fundamentals/testing``` <br />
761761
- Automated testing is considered an essential part of any serious software development effort. It is provides integration with Jest and Supertest out-of-the-box, while remaining agnostic to testing tools.
762762
763+
## Day 18 - NestJs-into (hello world)
764+
765+
1. It is mainly used to create Rest API, a NodeJS framework, builds on ExpressJs.
766+
2. Embraces TypeScript, dependency Injection and Modularity("Angular for Backend")
767+
3. Can be used to build MVC apps or Rest or GraphQl Apis.
768+
4. Enforces clean code and a clear project staructure by giving you a series of building blockes.
769+
5. Makes building complex application easy.
770+
771+
772+
6. Lets install NestJs first.
773+
774+
https://docs.nestjs.com/first-steps
775+
a) Open ```NodeJs command``` promt.
776+
Setting up a new project is quite simple with the Nest CLI. With npm installed. [It is similar to angular installation]
777+
<br>
778+
b) ```npm i -g @nestjs/cli```
779+
780+
c) Create new project using NEST
781+
```nest new project-name```
782+
e.g. nest new day18-NestIntro
783+
784+
it will ask for which package manager you want yo use for. I have chossen npm. Now it will install all th dependent packages.
785+
It takes 5 - 10 minute to install successfully.
786+
787+
<img src="https://github.com/getmscode2013/NodeJsTutorial/day18-nest-intro/NestPackaesInstalledsuccessfully.png">
788+
789+
7) Now open your application in visual studio code.
790+
a) now to see the diffrnce on the icon you can install extension "Material Icon Theme"
791+
b) so lets see the application structure.
792+
<img src="https://github.com/getmscode2013/NodeJsTutorial/day18-nest-intro/projectstructure.png">
793+
794+
- Node_module - consist all the package installed for application. - you can get details from package.json.
795+
- Src - is the mail folder on which we will work its contains all the source files.
796+
- Test - it is nothing but test setup for application. by default in jest.
797+
- .gitignore file - is file used to ignore the git uploads.
798+
- nest-cli.json file - It is nest configuration file.
799+
- package.json - contain all the installed module definations.
800+
- tsconfig.json - is type script configuratio files.
801+
802+
c) let see what we have in src.
803+
804+
- <b> main.ts <b/> - its entry point for application. nodejs not run typescript so code compile to javascript.
805+
if you will se the code here it is follow like expressJS.
806+
Creating the server and which will work on 3000 port.
807+
```ruby
808+
const app = await NestFactory.create(AppModule);
809+
await app.listen(3000);
810+
```
811+
- <b>app.module.ts </b>
812+
Nest is written the application in the moduler form like angular.
813+
As in above its written NEST is embraces modularity. Nest is not taken all the files in your working folder. it will take only which has imported their. <br />
814+
Imported part in NESTJs is controller and provide. As you need to list over here.
815+
```ruby
816+
controllers: [AppController], // bundle of controller
817+
providers: [AppService], // bundle of services
818+
```
819+
820+
> Controller : it is responsible to get the comming request and doing something and response it back.
821+
> Provider : it will inject the certain functionlaity to controller. it like service of database to get the data from database.
822+
823+
here you can see the ``` @module``` as the decorator which is attched to the class.
824+
825+
826+
- <b> app.controller.ts - </b>
827+
It is app class as AppController which is decorated by @controller.
828+
It is having method of getHello() decorated by @Get
829+
in Appcontroller you will find the code like below.
830+
831+
```ruby
832+
@Controller()
833+
export class AppController {
834+
```
835+
Means it will accept the empty parameter like
836+
your-domain.com/
837+
838+
if you have the parameter in controller like user
839+
```ruby
840+
@Controller('user')
841+
export class AppController {
842+
```
843+
you need to use it like your-domain.com/user
844+
845+
Same happan for the get also if it is like
846+
```ruby
847+
@Controller('User')
848+
export class AppController {
849+
constructor(private readonly appService: AppService) {}
850+
851+
@Get('Product')
852+
getHello(): string {
853+
return this.appService.getHello();
854+
}
855+
}
856+
```
857+
you need to use it like your-domain.com/user/product
858+
859+
Last thing is service. So here you are injecting the service to app.controller.
860+
```ruby
861+
constructor(private readonly appService: AppService) {}
862+
```
863+
this service should be registerd in the module. here you are declaring ut and using it by.
864+
```ruby
865+
this.appService.getHello();
866+
```
867+
868+
-<b> app.service.ts </b>
869+
870+
All your login aout the application or DB call are written to the service.
871+
It is using @Injectable decorator code for AppService class. And defined a method like getHello in service.
872+
```ruby
873+
getHello(): string {
874+
return 'Hello World!';
875+
}
876+
```
877+
Here you can write your complex logic.
878+
879+
- <b>app.controller.spec.ts </b>
880+
It used to write the unit test methods for app.controller
881+
882+
8) Run application
883+
Now let run and see the application.
884+
> cd day18-nest-intro
885+
> npm run start
886+
or to run with nodemon
887+
> npm run start:dev
888+
889+
<img src="https://github.com/getmscode2013/NodeJsTutorial/day18-nest-intro/helloworld.png">
890+
891+
it is showing "hello world" it is giving the result of string from service.
892+
Lets change it to some of the JSON values. Changes in controller directly with below code.
893+
894+
```ruby
895+
export class AppController {
896+
constructor(private readonly appService: AppService) {}
897+
898+
@Get()
899+
getHello(): {name: string,phoneNumber: number, addresses: string} {
900+
return {name: "Satya", phoneNumber: 1234567890, addresses : "test"};;
901+
}
902+
}
903+
```
904+
905+
<img src="https://github.com/getmscode2013/NodeJsTutorial/day18-nest-intro/jsonoutput.png">
906+
907+
You can even define the heder also for your API you need to import header.
908+
import { Controller, Get, Header } from '@nestjs/common';
909+
910+
and add the decorator @Hedader like below.
911+
```ruby
912+
@Get()
913+
@Header("Content-type",'test/html')
914+
getHello(): {name: string,phoneNumber: number, addresses: string} {
915+
return {name: "Satya", phoneNumber: 1234567890, addresses : "test1"};;
916+
}
917+
```
918+

0 commit comments

Comments
 (0)