Skip to content

Commit 0d0ba33

Browse files
committed
2 parents 4be2a68 + 789ccce commit 0d0ba33

File tree

1 file changed

+387
-0
lines changed

1 file changed

+387
-0
lines changed

README.md

Lines changed: 387 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,193 @@ Some other global object are
125125
- //Reports
126126
- //Process
127127

128+
## Day 4 - ECMA Script
129+
130+
ECMA was created to standardize JavaScript to help foster multiple independent implementations.
131+
The full form of ECMA is European Computer Manufacturer's Association.A standard for scripting languages like JavaScript, JScript is ECMAScript.
132+
133+
Here NodeJS follow the ECMA standard, here we are going to dicuss about Const, Let and class.
134+
> <b>let :</b> Unlike variables declared with var that are function-scoped, variables declared with let are block-scoped: they only exist in the block they are defined in.
135+
136+
```ruby
137+
var a = 20
138+
var a = 30;
139+
140+
console.log(a);
141+
142+
let x = 20;
143+
x = "satya"; /// but you can change the datatype
144+
//let x = 30; /// if you will defined like that it will give error
145+
146+
console.log(x);
147+
var v = () => {
148+
let x = 50; /// here again you can defined it and it will work like scoped variable.
149+
console.log(x);
150+
}
151+
v();
152+
153+
```
154+
Difference in Var and Let
155+
156+
157+
|Var | let |
158+
| ----------------------------------------------|--------------------------------------------------------------------------- |
159+
|variables belong to the global scope or local scope if they are declared inside a function: var counter;| The let variables are blocked scopes: let counter; |
160+
|var variables are added to the global object as properties. The global object is window on the web browser and global on Node.js: |However, the let variables are not added to the global object|
161+
|The var keyword allows you to redeclare a variable without any issue.| If you redeclare a variable with the let keyword, you will get an error|
162+
163+
164+
> <b>Const :</b> Variables defined with const behave like let variables, except they cannot be reassigned. <br />
165+
166+
If we assign a primitive value to a constant, we cannot change the primitive value:
167+
168+
169+
```ruby
170+
const a = 30;
171+
//a = 40; // you can not assign the a to another value
172+
173+
console.log(a);
174+
175+
```
176+
177+
You can change the properties of a constant object:
178+
179+
```ruby
180+
181+
// but if you defined the object and want to change the value of it tha you can do that
182+
183+
const obj = {name : "satya", phoneNo : 1234567890, email : "Test@gmail.com"}
184+
185+
console.log(obj);
186+
187+
obj.age = 20;
188+
console.log(obj);
189+
190+
obj.phoneNo = 1234512345;
191+
console.log(obj);
192+
```
193+
> <b>Class :</b> Its type of function, but instead of using function keyword to initiate it, here use the keyword class, and the properties are assigned inside a constructor() method. <br />
194+
195+
```ruby
196+
class TestUserClass
197+
{
198+
constructor()
199+
{
200+
this.name = "Satya first ";
201+
this.phoneNumber = "6776777"
202+
}
203+
204+
getName()
205+
{
206+
//console.log(name); // this will give error to access any of the class values you need to use thi.
207+
console.log(this.name);
208+
this.age = 12
209+
return this.name;
210+
}
211+
212+
getAge()
213+
{
214+
return this.age;
215+
}
216+
}
217+
218+
219+
var tc = new TestUserClass();
220+
//tc.getName();
221+
console.log(tc.getAge()); // you need to call the Get name first as the Age setting their.
222+
```
223+
224+
## Day 5 - Prototype
225+
226+
In the previous chapter we learned how to use an object constructor.
227+
228+
Sometimes you want to add new properties (or methods) to all existing objects of a given type.
229+
Sometimes you want to add new properties (or methods) to an object constructor.
230+
231+
Prototype property in Javascript allows you to add new properties to object constructors.
232+
233+
``` ruby
234+
// prototype usese to extend the functionalty of exiting function.
235+
236+
var Employee = function()
237+
{
238+
this.name = "Satya";
239+
this.age = 30;
240+
241+
242+
}
243+
244+
245+
var e = new Employee();
246+
console.log(e.age);
247+
248+
Employee.prototype = {
249+
phoneNumber : "4545454",
250+
251+
GetAge : function()
252+
{
253+
return this.name;
254+
}
255+
}
256+
257+
var e = new Employee();
258+
console.log(e.age);
259+
console.log(e.phoneNumber);
260+
console.log(e.GetAge());
261+
```
262+
263+
## Day 6 - Modules
264+
265+
1. Instead of keeping all the code in one file we split the code into multiple file.
266+
2. To splitting the file into mutiple file we defined it as modules.
267+
3. You can create your own modules, and easily include them in your applications.
268+
4. It will be useful for code resuability and for maintability. <br /><br />
269+
270+
Creating a samall calculator with is having addition, substartion and defined all in different file<br />
271+
272+
<b> addition.js </b>
273+
274+
```ruby
275+
// Addition module which need to export.
276+
277+
module.exports = {
278+
279+
Addition : function(a,b)
280+
{
281+
return a + b;
282+
}
283+
284+
}
285+
```
286+
287+
<b> subtration.js </b>
288+
289+
```ruby
290+
// you can define the modules like that as well as.
291+
// We define modules into two type.
292+
293+
var subtration = function(a,b)
294+
{
295+
return a -b;
296+
}
297+
298+
module.exports.subtration = subtration;
299+
```
300+
301+
<b> Calculator.js </b> < br/>
302+
Need to use require to export the module.
303+
304+
```ruby
305+
// exporting both of the module using require.
306+
307+
var add = require("./addition");
308+
var sub = require("./subtration");
309+
310+
console.log(`Addition of number 10 and 12 : ${add.Addition(10, 12)}`);
311+
console.log(`Subtration of number 14 and 12 : ${sub.subtration(14, 12)}`);
312+
```
313+
314+
128315
## Day 10 - ExpressJs
129316

130317
ExpressJs use to build the Node Application, it make faster the devlopment and provide inbuild code for development.<br /><br />
@@ -529,3 +716,203 @@ Plug that method which we need to attach that middle ware
529716
})
530717
531718
```
719+
720+
## Day 17 - NestJs
721+
722+
```https://docs.nestjs.com/``` <br />
723+
- It is used to build serverside application microservices and RestAPI.
724+
- It is proper framework like Asp.net, Django. It is provide you the architecture to build a server.
725+
- It is Extensible, Versatile and prograssive javascript.
726+
- If you are working with NestJs, type script is the default module. If you have worked with angular than NEST js is pretty similar.
727+
- Nest makes use of robust HTTP Server frameworks like Express (the default) and optionally can be configured to use Fastify as well!
728+
- Nest provides an out-of-the-box application architecture which allows developers and teams to create highly testable, scalable, loosely coupled, and easily maintainable applications.
729+
730+
```https://docs.nestjs.com/techniques/database``` <br />
731+
```https://docs.nestjs.com/recipes/sql-typeorm``` <br/>
732+
- Nest is database agnostic, allowing you to easily integrate with any SQL or NoSQL database
733+
- You can also directly use any general purpose Node.js database integration library or ORM,
734+
- Nest provides tight integration with TypeORM and Sequelize out-of-the-box with the @nestjs/typeorm and @nestjs/sequelize packages respectively, @nestjs/mongoose package for mango DB.
735+
736+
```https://docs.nestjs.com/techniques/authentication```
737+
- Passport is the most popular node.js authentication library, well-known by the community and successfully used in many production applications. It's straightforward to integrate this library with a Nest application using the @nestjs/passport module
738+
At a high level, Passport executes a series of steps to:
739+
a) Authenticate a user by verifying their "credentials" (such as username/password, JSON Web Token (JWT), or identity token from an Identity Provider)
740+
b)Manage authenticated state (by issuing a portable token, such as a JWT, or creating an Express session)
741+
c)Attach information about the authenticated user to the Request object for further use in route handlers
742+
743+
```https://docs.nestjs.com/techniques/logger``` <br />
744+
- Nest comes with a built-in text-based logger which is used during application bootstrapping and several other circumstances such as displaying caught exceptions (i.e., system logging). This functionality is provided via the Logger class in the @nestjs/common package.
745+
746+
```https://docs.nestjs.com/techniques/performance``` <br />
747+
- Nest also provides compatibility with other libraries such as, for example, Fastify.
748+
Fastify provides a good alternative framework for Nest because it solves design issues in a similar manner to Express. However, fastify is much faster than Express, achieving almost two times better benchmarks results.
749+
750+
```https://docs.nestjs.com/graphql/quick-start``` <br />
751+
- Harnessing the power of TypeScript & GraphQL.
752+
GraphQL is a powerful query language for APIs and a runtime for fulfilling those queries with your existing data. It's an elegant approach that solves many problems typically found with REST APIs.
753+
- GraphQL queries access not just the properties of one resource but also smoothly follow references between them. While typical REST APIs require loading from multiple URLs, GraphQL APIs get all the data your app needs in a single request. Apps using GraphQL can be quick even on slow mobile network connections.
754+
755+
```https://docs.nestjs.com/microservices/basics``` <br />
756+
- Nest natively supports the microservice architectural style of development.In Nest, a microservice is fundamentally an application that uses a different transport layer than HTTP.
757+
Nest supports several built-in transport layer implementations, called transporters, which are responsible for transmitting messages between different microservice instances. Most transporters natively support both request-response and event-based message styles.
758+
It uses package @nestjs/microservices.
759+
760+
```https://docs.nestjs.com/fundamentals/testing``` <br />
761+
- 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.
762+
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)