Skip to main content

NodeJS: Express JS

Express JS (MVC)

  • framework/architecture for nodejs (web)
  • express js as Rest API (get, post, put, delete - req. http method).
  • Htpp interaction communication (http req -> request response cycle)
  • middleware
  • input data, process, meaningful output (Web App)
  • npm install express -- save
Note:
- An object is equivalent to Javascript.
- NodeJS is equivalent to the callback.

- ExpressJS is equivalent to Middleware.

Creating Server in NodeJS and http module (Client Server Communication)
1. mkdir firstapp
2. cd firstapp
3. npm init
4. create app.js

Explanation (app.js):

- http module injection
- createServer method (callback function use -> req, res)
- After client and server connection, server will listen and client will request.
(req is always http req.object and res or 2nd argyment is always http response object.)
- req.end (end the request and listen the server (1st arg-> port, 2nd arg-> host and 3rd arg-> callback function.))
- If err -> server listening failed and if not server listening at port 4040 (to acknowledge the server status)


# Express Auto Folder Generator

  1. npm install -g express-generator (one time run)
  2. express folder_name (always run this code to build app)
  3. npm install (to install package mention in package.json)
  4. npm start (or edit "start": "nodemon app.js"- need to install nodemon)
  5. localhost: 3000 (run in browser)

Express 

- Express is routing and middleware web framework.
HTTP Method
  1. Get (default): req.data from a specified resource (create)
  2. Post: send data to the server to create/update (read)
  3. Put: send data to a server to create/ update resource (rename)
  4. Delete: delete specified resource (delete)
#Postman
Postman contains a powerful runtime based on Nodejs that allows you to add dynamic behavior to requests and collections. 
- API Development Environment.
- save, import, export, env., link share
- unique, username, email (null value -> unique)

#Middleware
- ExpressJS is a lightweight, efficient middleware and routing framework.
- Middleware functions are functions that have access to the request object (req) and the response object (res) and the next function in the applications req-response cycle.
- Everything in the express is middleware.
Example:
var a = function (req, res, next){
}
app.use(a);
app.get(a);

Types
1. Application Level middleware
var app.express()
app.use(function(req, res, next){
     console.log('hi');
     next();
});

2. Routing Level middleware
Example: 
app.use('/auth', function (req, res, next){
});

3. Inbuilt Level middleware
Program -> inactive state
Process -> active state (thread- execution sequence of process eg: save, delete, edit).
- NodeJS is single threaded programming language. Process is keyword that holds running process instance. 0-> false, 1-> true
Example:
var path = require('path);
app.use('images', express.staet(path.join __dirname, 'images'))

4. Third Part Level middleware (available in nodejs.com)
Example: Morgan (req. logger middleware for node js)

5. Error Handling Level middleware
Example:
app.use(function(err, req, res, next){
     console.log('Err is', err);
     res.json({
             status: err.status || 400,
             msg: err.message || "Wrong"
})
})

Description:
- it doesnt self invoked in http request cycle
- err handling middleware needs to be called execute
- call garne method next with argument
- next is middleware function
- next without arg calls another successive control
- next with arg le sidai error handling middle ware call garxa

#Templating Engine
- HTML doesn't bind data so we use template engine.
- Example: Pug, Handlebars, Ejs, Jade, Mustache


# JWT(Json Web Token)
- is open industry standard RFC 7519 method for representing claims securely between 2 parts. JWT allows you to decode, verify and generate
Parts:
1. headers (algorithms and token type)
2. Signature (secret key)
3. data (payload)

# Error Handling
- Garbage collection and error handling (memory usage release). memory usage handle
Example:
- Var A (locate memory), if its usage is over, we can make null value in A variable, this is called garbage.

Comments

Popular posts from this blog

Javascript: Object to Array and Array to Object

Output: Final Array [ { mango: 1 }, { apple: 2 }, { banana: 3 }, { orange: 1 }, { watermelon: 1 }, { papaya: 1 } ] Output: Final Object { mango: 1, apple: 2, banana: 3, orange: 1, watermelon: 1, papaya: 1 }

Javascript: Callback and High Order Function

CALLBACK A callback is a function that gets invoked after an asynchronous result appears. a callback function is an async function argument. - Asynchronous code result handle using callback and promises. // callback  function function with argument  // ---argument :// if a function pass-through function as an argument, it is higher-order function. function needs to be called in order to execute. a callback is used when calling a function asynchronous  What is a Callback or Higher-order Function? A callback function , also known as a higher-order function, is a function that is passed to another function (let’s call this other function “otherFunction”) as a parameter, and the callback function is called (or executed) inside the otherFunction. A callback function is essentially a pattern (an established solution to a common problem), and therefore, the use of a callback function is also known as a callback pattern. High-order function is a function that takes ...

Javascript: Frontend - AngularJS

# FrontEnd Technologies: # Web (Internet) - Html, css framework (bootstrap, material design-> component based) Angular 2 - Single page application (fast). - Web application or website that interacts with the user by dynamically rewritting the current page rather than loading entire new pages from a server. - Can do asynchronous jobs. # Tools: - Typescript - VS code # MVVW (Model View ViewModel Pattern) -> two-way data binding - In angular, the controller (the C in MVC) is replaced by ViewModel (the VM in MVVM). # Typescript Programming Language Typescript (.ts) is imp in angular 2. - supersetup js - strictly typed program - can do class based oop - future programming language. - security maintain (TS code compile and run in js) - JS with additional features. # Data Binding - Controller and Views synchronize. Types: 1. Event -  eg: click, onClick, change 2. Property - eg: hide, show 3. 2-way binding - Reflect in view if changes in model- c...