Skip to content

Instantly share code, notes, and snippets.

@nikkaroraa
Last active June 11, 2017 17:54
Show Gist options
  • Save nikkaroraa/8a2d7ced4c0979275d6e53351d0b2974 to your computer and use it in GitHub Desktop.
Save nikkaroraa/8a2d7ced4c0979275d6e53351d0b2974 to your computer and use it in GitHub Desktop.
1. Process exists in node shell but not in browser
Process gives access to the process of node shell
process.exit(0)
2. this in shell equals global object
this == global
window is an alias of global in browser
// JS is an implementation of ECMAScript
3. Initially module.exports == {};
//exported.js
console.log(module.exports)
module.exports = {
a:10
};
Prints console.log when imported.js is run because JS is implemented.
The file that is imported into the secondary file, gets implemented line by line returning only the
module.exports at the end.
require('./exported.js'); -> returns the module.exports object
4. Script mode and Modular mode
--> REPL runs in script mode
this == global in script mode
--> node filename.js runs in modular mode
this = {}; //initially
as this is the current object for each module
which is blank in the start
5. function fun(){var p =10;}
fun()
p == 10; //reference error
-- function fun(){var q =10;}
var b = new fun();
q //reference error
-- function fun(){q =10;}
var b = new fun();
q //no error
Traverses up the scope till it doesn't find and finally creates the variable in the top most scope.
Window in browser case
6. //exported.js
var k = "String";
//module.exports make the things (state/method) defined inside it, PUBLIC!
module.exports = {
a:10,
returned: function(){
return k;
}
};
//imported.js
const files = require('./exported.js');
console.log(files);
console.log(files.returned()); //->"String"
console.log(files.k) //doesn't output
//k is private
7. //exported.js
k = "String";
module.exports = {
a:10,
returned: function(){
return k;
}
};
//imported.js
const files = require('./exported.js');
console.log(files);
console.log(files.returned()); //->"String"
console.log(k) //->returns because of the non-strict mode, assignment to the global object
//Traverses and if not found in any, defines the variable in the top-most scope. Global in this case.
This is a bad practice. That is why we include "use strict" at the start of every file.
"use strict";
Now k will be undefined.
8. Strict mode doesn't declare variables implicitly.
"use strict" can be used in any scope
Local or Global
Introduced in ECMAScript 5
Used for prevention of the practice of polluting the Global Scope.
//Try to define only those objects in the Global scope that reaaly need to be defined there.
9. this == module.exports //initially
//this object can't be changed. Whereas module.exports object can be changed.
var k = "String";
console.log(this == module.exports) //--> true
module.exports = {
a:10,
returned: function(){
return k;
}
};
this==module.exports //after assignment, this expression returns false
const files = require('./exported.js');
console.log(files);
10. Just like module.exports, this object can also be returned (in the imported.js) when used like this:
this.a = 5;
this.returned = function(){
return a;
}
this also gets returned to the file which is importing this.
But this practice is termed as a bad practice.
11. NPM (Node Package Manager)
-- What is it?
npm is a package manager for Node.js with hundreds of thousands of packages. Although it does create some of your directory structure/organization, this is not the main purpose.
The main goal, as you touched upon, is automated dependency and package management. This means that you can specify all of your project's dependencies inside your package.json file, then any time you (or anyone else) needs to get started with your project they can just run npm install and immediately have all of the dependencies installed. On top of this, it is also possible to specify what versions your project depends upon to prevent updates from breaking your project.
It is definitely possible to manually download your libraries, copy them into the correct directories, and use them that way. However, as your project (and list of dependencies) grows, this will quickly become time-consuming and messy. It also makes collaborating and sharing your project that much more difficult.
Hopefully this makes it more clear what the purpose of npm is. As a Javascript developer (both client-side and server-side), npm is an indispensable tool in my workflow.
-- How to use it?
sudo npm install -g <package-name>
-g or --global flag are same for global installation of npm packages
npm install -s install <package-name> //to save the dependencies in package.json
npm install --save install <package-name> //to save the dependencies in package.json
12. ExpressJS
//generally
const express = require('./node_modules/express')
//but this can also be used
const express = require('express') //returns a function
const app = express(); //creates the server
app.listen(2222, function(){ //asynchronous second attribute, called whenever the server is ready.
console.log('Our server is running on http://localhost:2222')
}); //0 to 1024 ports are reserved by system.
13. General way of writing a port number with domain
domain.com:port number
For example:
google.com:80
scheme/protocol (https), domain (google.com), path (/search), query (q=js)
https://google.com/search?q=js
14. Routes
15. Middlewares: Servers are created with the help of multiple middlewares
//For personal knolwedge: gzip is used for compression and decompression on the net.
app.use //for using middlewares
Middlewares are like a set of gates, where each gate is used to check or modify the request and pass it on to the next.
The middlewares can change the data that is passed on to them and then pass that
modified data to the next middleware.
For example: Just like in case of decompression and decryption middleware.
As soon as response is sent, it cannot be modified as expected.
//Defining middlewares
function m1(req, res, next){
console.log('m1');
//res.send('m1');
next();
}
function m2(req, res, next){
console.log('m2');
res.send('m2');
next();
}
function m3(req, res, next){
console.log('m3');
//res.send('m3');
next();
}
app.use(m1);
app.use(m2);
app.get('/', function(req, res){
console.log('sending response');
res.send("Hi");
});
app.use(m3);
app.listen(2222, function(){ //asynchronous second attribute
console.log('Our server is running on http://localhost:2222');
});
16. statusCode
300 series: Redirection
400 series: Not Found
500 series: Database error
200 == OK
17. var alpha = function(){
}
alpha.k = 2;
//alpha can be used for both functions as its keys as properties.
18. Arrow Function Expression (started from ES6)
//Traditional function definition
var multiply = function(x,y){
return(x * y);
}
//Function definition with Arrow Function Expression
var mutliply = (x, y) => {return x*y};
Curly brackets are not required if only one expression is present. The preceding example could also be written as:
var multiply = (x, y) => x*y;
Refer: https://www.sitepoint.com/es6-arrow-functions-new-fat-concise-syntax-javascript/
-- Assignment
Create an express app
Use object to store in the back-end instead of localStorage
bodyParser package
add bodyParser middleware
-- Day5 code on: cb.lk/nodeintro
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment