- const vs var vs let
const : block scope, constant value in block let : block scope var : function scope
- Arrow functions
let arrFunc1 = (arg1, arg2, arg3) => {
}
// OR
let arrFunc2 = arg1 => console.log('Some one line code instead of console.log');
// OR
let arrFunc2 = (arg1, arg2) => console.log('Some one line code instead of console.log');
- Promises
let somePromise = new Promise((resolve, reject) => {
// if successful execution
resolve(result);
// else
reject(err);
});
somePromise
.then((result) => {
// todo next step after getting result
})
.catch((error) => {
// handle error
});
;
- require()
const http = require('http'); // default Node module/ module inside node_modules folder
const myModule1 = require('path/to/file/relative/dir/myModule1.js');
const myModule2 = require('path/to/file/relative/dir/myModule2'); //No need to mention .js extension
const myModule3 = require('path/to/file/relative/dir/'); //No need to mention index.js for including index.js
- Request is received by the server by the server.serve method (http.createServer())
- _getRequestParams obtains the whole request object in the required format
- We choose a handler from our existing set of handlers as per the url and method
- We call the handler and let it handle the request
- Handler makes additional DB/API requests if needed
- Once the handler handles the request, it callbacks with an appropriate response object and status
- _generateResponse() just writes the result object and status to HTTP response
- _getRequestParams recieves payload in pieces, so that takes place over a period asynchronously. Thus _getRequestParams returns a Promise
- the handler may make DB/API requests that happen in the background asynchronously. Thus every handler returns a Promise of handling the request
There are lot of HTTP methods a) GET : get data/resource b) POST: create new data/resource c) PUT : update existing data/resource d) DELETE: delete existing data/resource e) PATCH: partial update of data
NOTE: It is possible to interchangebly use these methods, and a lot of APIs actually end up doing that, these are just recommended standards
www.example.com/urlpath/for/some/task
urlpath: /urlpath/for/some/task
syntax wise it looks like linux's path syntax
www.example.com/urlpath/for/some/task?param1=val1¶m2=val2
queryParams: {param1: val1, param2 : val2}
Additional parameters you can add to the overall url, used a lot in GET. Multiple params are separated by a '&' Sometimes the | is also used to pass a list of values like for
param1 : [val1,val2,val3]
?param1=val1|val2|val3
However I found this syntax isn't widely used
- Provide key-value pair information
- Usually used for userTokens and authentication
- Payload type like application/json can also be mentioned
- Many websites use specific Cache Headers to instruct browser how to cache the site
Possible payloads
- Form data (key-value based)
- File uploads (raw data)
- Structured data like JSON, XML
projectDir/
.git
.gitignore
index.js
lib/
config/
index.js // Main config setup
cert.pem // http
key.pem
handlers/
index.js // Main handler
server.js