From 3efedb4deda93f114bfcb86d61b29e701e8868ae Mon Sep 17 00:00:00 2001 From: Zoheb Alli Khan Date: Thu, 3 Jun 2021 08:06:46 +0530 Subject: [PATCH 01/13] Two methods added --- backend/express.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 backend/express.js diff --git a/backend/express.js b/backend/express.js new file mode 100644 index 0000000..2a6b56f --- /dev/null +++ b/backend/express.js @@ -0,0 +1,51 @@ +/* ******************************************************************************************* + * API + * http://expressjs.com/en/api.html + * ******************************************************************************************* */ + +const express = require("express"); // Importing the express library. +const app = express(); // Intializing the imported express application + +/* ******************************************************************************************* + * GLOBAL OBJECTS + * http://expressjs.com/en/api.html#express.json + * ******************************************************************************************* */ + +Methods; + +express.json([options]); + +options: ` +inflate // to manage the deflated bodies like enabling and disabling + +limit // Controls the maximum request body size. + +reviver // It is passed directly to JSON.parse as an second argument + +type // This is used to determine the type of middleware will parse + +verify // It is an undefined function which used to verify the middleware parsing. + +`; + +express.raw([options]); + +options: ` +inflate // to manage the deflated bodies like enabling and disabling + +limit // Controls the maximum request body size. + +type // This is used to determine the type of middleware will parse + +verify // It is an undefined function which used to verify the middleware parsing. +`; + +express.Router([options]); + +options: ` +caseSensitive //Enables case sensitivity + +mergeParams //if param names of child and parent are conflicted then the child takes the precedence + +strict // Enables Strict routing +`; From 81add6e07b15939bafb6e6ff585b682aa2dd1669 Mon Sep 17 00:00:00 2001 From: Zoheb Alli Khan Date: Thu, 3 Jun 2021 09:54:44 +0530 Subject: [PATCH 02/13] Added all express methods --- backend/express.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/backend/express.js b/backend/express.js index 2a6b56f..9c9987c 100644 --- a/backend/express.js +++ b/backend/express.js @@ -49,3 +49,51 @@ mergeParams //if param names of child and parent are conflicted th strict // Enables Strict routing `; + +express.static(root, [options]); + +options: ` +dotfiles // determines how dotfiles are used + +etag // Operates etag generation + +extensions // Operates file extension fallback + +fallthrough // Enable or disable the immutable directive in the Cache-Control response header + +index // sends the specified directory index file + +LastModified // sets the Last-Modified header to the last modified date + +setHeaders // Function for setting HTTP headers to serve with the file +`; + +express.text([options]); + +options: ` +defaultCharset // Sets the default charset for the text context. + +inflate // to manage the deflated bodies like enabling and disabling + +limit // Controls the maximum request body size. + +type // This is used to determine the type of middleware will parse + +verify // It is an undefined function which used to verify the middleware parsing. +`; + +express.urlencoded([options]); + +options: ` +extended // it allows to choose between parsing the URL-encoded data or the qs library + +parameterLimit // It controls the no of params. + +inflate // to manage the deflated bodies like enabling and disabling + +limit // Controls the maximum request body size. + +type // This is used to determine the type of middleware will parse + +verify // It is an undefined function which used to verify the middleware parsing. +`; From 9731e6841b46e4c4252e5811119a54fb0d4f0382 Mon Sep 17 00:00:00 2001 From: Zoheb Alli Khan Date: Thu, 3 Jun 2021 09:59:08 +0530 Subject: [PATCH 03/13] Structured Components --- backend/express.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/backend/express.js b/backend/express.js index 9c9987c..f3e1c8c 100644 --- a/backend/express.js +++ b/backend/express.js @@ -16,28 +16,28 @@ Methods; express.json([options]); options: ` -inflate // to manage the deflated bodies like enabling and disabling +inflate // to manage the deflated bodies like enabling and disabling -limit // Controls the maximum request body size. +limit // Controls the maximum request body size. -reviver // It is passed directly to JSON.parse as an second argument +reviver // It is passed directly to JSON.parse as an second argument -type // This is used to determine the type of middleware will parse +type // This is used to determine the type of middleware will parse -verify // It is an undefined function which used to verify the middleware parsing. +verify // It is an undefined function which used to verify the middleware parsing. `; express.raw([options]); options: ` -inflate // to manage the deflated bodies like enabling and disabling +inflate // to manage the deflated bodies like enabling and disabling -limit // Controls the maximum request body size. +limit // Controls the maximum request body size. -type // This is used to determine the type of middleware will parse +type // This is used to determine the type of middleware will parse -verify // It is an undefined function which used to verify the middleware parsing. +verify // It is an undefined function which used to verify the middleware parsing. `; express.Router([options]); From 859556dc1ff8b5ac475cf94f74b2bc55c147feba Mon Sep 17 00:00:00 2001 From: Zoheb Alli Khan Date: Thu, 3 Jun 2021 12:04:27 +0530 Subject: [PATCH 04/13] App Properties --- backend/express.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/backend/express.js b/backend/express.js index f3e1c8c..935eb1f 100644 --- a/backend/express.js +++ b/backend/express.js @@ -11,7 +11,7 @@ const app = express(); // Intializing the imported express application * http://expressjs.com/en/api.html#express.json * ******************************************************************************************* */ -Methods; +```Methods```; express.json([options]); @@ -97,3 +97,10 @@ type // This is used to determine the type of middleware will verify // It is an undefined function which used to verify the middleware parsing. `; + +```Application`````; +Properties```app.local`; + +`app.locals.title = "My Cheatsheet"; +console.dir(app.locals.title)`; +app.mountpath`;``app.mountpath``const admin = express()`; From 24faa1e1c15ba28dbd0cf601122c58f5070a1148 Mon Sep 17 00:00:00 2001 From: Zoheb Alli Khan Date: Thu, 3 Jun 2021 14:20:43 +0530 Subject: [PATCH 05/13] Changes --- backend/express.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/backend/express.js b/backend/express.js index 935eb1f..740bb7b 100644 --- a/backend/express.js +++ b/backend/express.js @@ -102,5 +102,9 @@ verify // It is an undefined function which used to verify the Properties```app.local`; `app.locals.title = "My Cheatsheet"; + console.dir(app.locals.title)`; -app.mountpath`;``app.mountpath``const admin = express()`; + +app.mountpath` + +``app.mountpath``const admin = express()`; From b9be6c4cbcb474a2232bca820106ebe05db3599d Mon Sep 17 00:00:00 2001 From: Zoheb Alli Khan Date: Thu, 3 Jun 2021 23:14:49 +0530 Subject: [PATCH 06/13] Added some cheatsheets --- backend/express.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/backend/express.js b/backend/express.js index 740bb7b..6796340 100644 --- a/backend/express.js +++ b/backend/express.js @@ -103,8 +103,20 @@ Properties```app.local`; `app.locals.title = "My Cheatsheet"; -console.dir(app.locals.title)`; +console.dir(app.locals.title)`; // Creating objects with local variables app.mountpath` -``app.mountpath``const admin = express()`; +``app.mountpath``const admin = express() +admin.get('/', function(req,res){ + console.log(admin.mountpath) + res.send('Admin Homepage') +}) + +app.use('', admin)`; // Mounting a sub - app + +``Event`` + +`admin.on('mount', (parent){ + console.log('Admin Mounted') +})` // \ No newline at end of file From 8008c44d4915d05ff4c1ac5df37e058367344e61 Mon Sep 17 00:00:00 2001 From: Zoheb Alli Khan Date: Fri, 4 Jun 2021 09:54:29 +0530 Subject: [PATCH 07/13] HTTP App Routes --- backend/express.js | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/backend/express.js b/backend/express.js index 6796340..a06fc0a 100644 --- a/backend/express.js +++ b/backend/express.js @@ -119,4 +119,43 @@ app.use('', admin)`; // Mounting a sub - app `admin.on('mount', (parent){ console.log('Admin Mounted') -})` // \ No newline at end of file +})` // Mounting on a parent app + +``Methods`` +`app.get('/', function(req, res){ + res.send('GET request to message') +})` // get requests to the specified path + +`app.post('/', function(req,res){ + res.send('POST request to a webpage') +})` + +`app.put('/', function(req,res){ + res.send('PUT request to a webpage') +})` + +`app.delete('/', function(req,res){ + res.send('DELETE request to a webpage') +})` + +`app.all('/', function(req,res,next){ + console.log('Accessing the secret section....') + next() +})` + +`app.param('user', function(req,res,next){ + User.find(id, function(err, user){ + if(err){ + next(err) + } else if (user){ + req.user = user + next() + } else { + next(new Error('Failed to load user')) + } + }) +})` + +`app.use(function(req,res,next){ + res.send('Hey There!') +})` \ No newline at end of file From e775f8e6757be753fe49cf665740395a8751f81d Mon Sep 17 00:00:00 2001 From: Zoheb Alli Khan Date: Fri, 4 Jun 2021 10:45:05 +0530 Subject: [PATCH 08/13] Request Method and Props --- backend/express.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/backend/express.js b/backend/express.js index a06fc0a..6814152 100644 --- a/backend/express.js +++ b/backend/express.js @@ -158,4 +158,24 @@ app.use('', admin)`; // Mounting a sub - app `app.use(function(req,res,next){ res.send('Hey There!') -})` \ No newline at end of file +})` + +```Request``` +``Methods`` + +`req.param('name')` + +``app.post('/', function (req, res, next) { + console.log(req.body) + res.json(req.body) +})`` + +`console.dir(req.cookies.name)` + +`console.dir(req.query.q)` + +`console.log(req.route) +res.send('GET')` + +`console.dir(req.signedCookies.user)` + From b995f9e2c410b41ff5deb584122af372b372f527 Mon Sep 17 00:00:00 2001 From: Zoheb Alli Khan Date: Fri, 4 Jun 2021 10:47:31 +0530 Subject: [PATCH 09/13] Distinction --- backend/express.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/backend/express.js b/backend/express.js index 6814152..0ea6cd6 100644 --- a/backend/express.js +++ b/backend/express.js @@ -165,10 +165,12 @@ app.use('', admin)`; // Mounting a sub - app `req.param('name')` -``app.post('/', function (req, res, next) { +``Properties`` + +`app.post('/', function (req, res, next) { console.log(req.body) res.json(req.body) -})`` +})` `console.dir(req.cookies.name)` From 106d6565675da317e813240327fd9f0b9e97188f Mon Sep 17 00:00:00 2001 From: Zoheb Alli Khan Date: Fri, 4 Jun 2021 11:41:30 +0530 Subject: [PATCH 10/13] Response methods --- backend/express.js | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/backend/express.js b/backend/express.js index 0ea6cd6..7b082cf 100644 --- a/backend/express.js +++ b/backend/express.js @@ -163,9 +163,22 @@ app.use('', admin)`; // Mounting a sub - app ```Request``` ``Methods`` -`req.param('name')` +`req.get('content-type')` +`req.accepts('html')` + + `req.is('json')` + + `var range = req.range(1000) + if (range.type === 'bytes'){ + range.forEach(function(r){ + + }) + }` + ``Properties`` + +`req.param('name')` `app.post('/', function (req, res, next) { console.log(req.body) @@ -181,3 +194,24 @@ res.send('GET')` `console.dir(req.signedCookies.user)` + +```Response``` +``Methods`` + +`res.redirect('https://google.com')` + +`res.send({message: 'Awesome Stuffs'})` + +`res.json({alert: 'awesomecheatsheets'})` + +`const file = req.params.name; +res.sendFile(file, options, function(err){ + if(err){ + next(err) + }else{ + console.log('Sent:', file) + } +})` + +`res.render('index')` + From 862c1138923adc96c3bef6752dec31a5cb2e528a Mon Sep 17 00:00:00 2001 From: Zoheb Alli Khan Date: Fri, 4 Jun 2021 11:51:15 +0530 Subject: [PATCH 11/13] BodyParser --- backend/express.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/backend/express.js b/backend/express.js index 7b082cf..ebf7379 100644 --- a/backend/express.js +++ b/backend/express.js @@ -215,3 +215,10 @@ res.sendFile(file, options, function(err){ `res.render('index')` +```BodyParser``` + +`const BodyParser = require('body-parser') +app.use(BodyParser.json()) +app.use(BodyParser.urlencoded({ + extended: true +}))` \ No newline at end of file From 2fa3e1b155a31cb03a8eddfbe2a8940d1a107475 Mon Sep 17 00:00:00 2001 From: Zoheb Alli Khan Date: Fri, 4 Jun 2021 16:17:37 +0530 Subject: [PATCH 12/13] Whole Cheatsheet --- backend/express.js | 76 ++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/backend/express.js b/backend/express.js index ebf7379..d919bfb 100644 --- a/backend/express.js +++ b/backend/express.js @@ -2,6 +2,8 @@ * API * http://expressjs.com/en/api.html * ******************************************************************************************* */ +`npm i express --save` or`yarn add -D express``(-D saves it as a dev dependency)` +`yarn add -D @types/express``(Installing for TS)` const express = require("express"); // Importing the express library. const app = express(); // Intializing the imported express application @@ -13,7 +15,7 @@ const app = express(); // Intializing the imported express application ```Methods```; -express.json([options]); +`express.json([options]);` options: ` inflate // to manage the deflated bodies like enabling and disabling @@ -28,7 +30,7 @@ verify // It is an undefined function which used to verify th `; -express.raw([options]); +`express.raw([options]);` options: ` inflate // to manage the deflated bodies like enabling and disabling @@ -40,7 +42,7 @@ type // This is used to determine the type of middleware wi verify // It is an undefined function which used to verify the middleware parsing. `; -express.Router([options]); +`express.Router([options]);` options: ` caseSensitive //Enables case sensitivity @@ -50,7 +52,7 @@ mergeParams //if param names of child and parent are conflicted th strict // Enables Strict routing `; -express.static(root, [options]); +`express.static(root, [options]);` options: ` dotfiles // determines how dotfiles are used @@ -68,7 +70,7 @@ LastModified // sets the Last-Modified header to the last modified d setHeaders // Function for setting HTTP headers to serve with the file `; -express.text([options]); +`express.text([options]);` options: ` defaultCharset // Sets the default charset for the text context. @@ -82,7 +84,7 @@ type // This is used to determine the type of middleware will verify // It is an undefined function which used to verify the middleware parsing. `; -express.urlencoded([options]); +`express.urlencoded([options]);` options: ` extended // it allows to choose between parsing the URL-encoded data or the qs library @@ -103,7 +105,7 @@ Properties```app.local`; `app.locals.title = "My Cheatsheet"; -console.dir(app.locals.title)`; // Creating objects with local variables +console.dir(app.locals.title)`; `// Creating objects with local variables` app.mountpath` @@ -113,35 +115,35 @@ admin.get('/', function(req,res){ res.send('Admin Homepage') }) -app.use('', admin)`; // Mounting a sub - app +app.use('', admin)`; `// Mounting a sub - app` ``Event`` `admin.on('mount', (parent){ console.log('Admin Mounted') -})` // Mounting on a parent app +})` `// Mounting on a parent app` ``Methods`` `app.get('/', function(req, res){ res.send('GET request to message') -})` // get requests to the specified path +})` `// get requests to the specified path` `app.post('/', function(req,res){ res.send('POST request to a webpage') -})` +})` `// post request to the specified path` `app.put('/', function(req,res){ res.send('PUT request to a webpage') -})` +})` `// post request to the specified path` `app.delete('/', function(req,res){ res.send('DELETE request to a webpage') -})` +})` `// delete request to the specified path` `app.all('/', function(req,res,next){ console.log('Accessing the secret section....') next() -})` +})` `// Routing all types of HTTP request` `app.param('user', function(req,res,next){ User.find(id, function(err, user){ @@ -154,55 +156,55 @@ app.use('', admin)`; // Mounting a sub - app next(new Error('Failed to load user')) } }) -})` +})` `// Adding callback trigger to route parameters` `app.use(function(req,res,next){ res.send('Hey There!') -})` +})` `// To Invoke the middleware layer that you want to add` ```Request``` ``Methods`` -`req.get('content-type')` +`req.get('content-type')` `// Returns the specified HTTP req header` -`req.accepts('html')` +`req.accepts('html')` `// Checks if the specified content types are available or not` - `req.is('json')` +`req.is('json')` `// Requests the matching content-type` - `var range = req.range(1000) - if (range.type === 'bytes'){ - range.forEach(function(r){ - - }) - }` +`var range = req.range(1000) +if (range.type === 'bytes'){ + range.forEach(function(r){ + // Your code + }) +}` `// Range header parser` ``Properties`` -`req.param('name')` +`req.param('name')` `// Requests the param name when present` `app.post('/', function (req, res, next) { console.log(req.body) res.json(req.body) -})` +})` `// Data submitted in the request body` -`console.dir(req.cookies.name)` +`console.dir(req.cookies.name)` `// Contains cookies sent by the request` -`console.dir(req.query.q)` +`console.dir(req.query.q)` `// Query string parameter in the route` `console.log(req.route) -res.send('GET')` +res.send('GET')` `// Outputs all the layer, methods, path` -`console.dir(req.signedCookies.user)` +`console.dir(req.signedCookies.user)` `// Logs all the signed cookies sent by the request` ```Response``` ``Methods`` -`res.redirect('https://google.com')` +`res.redirect('https://google.com')` `// Redirects to the intended page` -`res.send({message: 'Awesome Stuffs'})` +`res.send({message: 'Awesome Stuffs'})` `// Response to the webpage` -`res.json({alert: 'awesomecheatsheets'})` +`res.json({alert: 'awesomecheatsheets'})` `// Response in JSON type` `const file = req.params.name; res.sendFile(file, options, function(err){ @@ -211,9 +213,9 @@ res.sendFile(file, options, function(err){ }else{ console.log('Sent:', file) } -})` +})` `// Sends file to the intended path` -`res.render('index')` +`res.render('index')` `// Rendering the intended file` ```BodyParser``` @@ -221,4 +223,4 @@ res.sendFile(file, options, function(err){ app.use(BodyParser.json()) app.use(BodyParser.urlencoded({ extended: true -}))` \ No newline at end of file +}))` `// Parses incoming request bodies` \ No newline at end of file From d4fe30f94ba2dedbcb1f524de726945b3a74d859 Mon Sep 17 00:00:00 2001 From: Zoheb Alli Khan Date: Fri, 4 Jun 2021 16:38:50 +0530 Subject: [PATCH 13/13] Added in Readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5436080..cd2aa74 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ Feel free to take a look. You might learn new things. They have been designed to #### Javascript - [Adonis.js](backend/adonis.js) +- [Express.js](backend/express.js) - [Feathers.js](backend/feathers.js) - [Moleculer](backend/moleculer.js) - [Node.js](backend/node.js)