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

Deploy react app on digitalocean using nginx

Source:  https://www.learnwithjason.dev/blog/deploy-nodejs-ssl-digitalocean/ Source:  https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04 Source:  https://www.youtube.com/watch?v=vRrhQwNixlc https://www.youtube.com/watch?v=pyxXCwgWdMw https://dev.to/xarala221/the-easiest-way-to-deploy-a-react-web-application-2l8a 1. Create a new droplet on DigitalOcean. Choose the $5/month option with Ubuntu 16.04.1 x64. Select a region closest to your users. 2. Finally, add your SSH key and ls -la ~/.ssh # This copies the key so you can paste with command + V pbcopy < ~/.ssh/id_rsa.pub # This prints it in the command line for manual copying cat ~/.ssh/id_rsa.pub   3.  Add your SSH key to the droplet Click “Add SSH Key” to save it, then make sure it’s selected, name your droplet, and hit the big “Create” button to get your server online. Your new droplet will display

Gallery

https://fancyapps.com/fancybox/3/ <!-- 1. Add latest jQuery and fancybox files --> <script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script> <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" /> <script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script> $ ( function () { if ( $ ( "[data-fancybox]" ). length ){ $ ( "[data-fancybox]" ). fancybox (); } });

Javascript: Array

ARRAY // homogenious and heterogenious array  var a = 'I am here'; var status = true; //array -> multiple values var arr = new Array(); //constructor method var arr1 = []; //bracket notation Example: var hobbies = ['dancing', true, 3423,{ //multiple type data collection values dance: true, singing: false }, []];