GoConqr hat ein neues Design! Sag uns, wie es dir gefällt.

Notizen

Nodejs

Omkar Gan Blockchain App development Englisch 3 Seiten

Inhalt der Notizen (3)

Seite 1

INTRODUCTION

What is Nodejs?

javascript runtime environment running chrome's V8 javascript engine in background

runs single-threaded, non-blocking, asynchronously programming, which is very memory efficient

Node.js eliminates the waiting, and simply continues with the next request

Nodejs is used to make real time applications like chat app because of it's high speed and non-blocking features

A typical nodejs development environment includes,

Tools like nvm (node version manager), npm/yarn (package managers) and external modules installed using package managers

Seite 2

JS tutorial

https://learnxinyminutes.com/docs/javascript/

https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript

5 / 2; // = 2.5

// Floor division

Math.floor(5/2); // =2

// Exponentiation

Math.pow(2,3); // =8

// There are three special not-a-real-number values:

Infinity; // result of e.g. 1/0

-Infinity; // result of e.g. -1/0

NaN; // result of e.g. 0/0, stands for 'Not a Number'

// Other data types

null; // deliberately assigned for a non-value initialization

undefined; // uninitialized value

// There's also a boolean type.

true;

false;

// parsing a numeral string

parseInt('123', 10); // 123

parseInt('010', 10); // 10

parseInt('11', 2); // 3

// Number.isNaN() to determine whether a given argument is a number or not

Number.isNaN('1'); // false

Number.isNaN(NaN); // true

//string operations

'hello'.length; // 5

'hello'.charAt(0); // "h"

'hello, world'.replace('world', 'mars'); // "hello, mars"

'hello'.toUpperCase(); // "HELLO"

// 0, empty strings (""), NaN, null, and undefined => false

// rest others => true

// declaring variables

let a; // = undefined

const a

'hello' + ' world'; // "hello world"

// from left to right if string is added to a number string concatenation is performed

'3' + 4 + 5; // "345"

3 + 4 + '5'; // "75"

// comparisons

123 == '123'; // true

1 == true; // true

123 === '123'; // false

1 === true; // false

for (let value of array) {

// do something with value

}

for (let property in object) {

// do something with object property

}

// Objects

// Type 1

let a = new Object(); // Object is defined in JS

// Type 2 (literal)

let a = {

name: "john fernandes",

id: 1234

}

// Example and usage

var obj = {

name: 'Carrot',

_for: 'Max', // 'for' is a reserved word, use '_for' instead.

details: {

color: 'orange',

size: 12

}

};

obj.details.color; // orange

obj['details']['size']; // 12

// Arrays

// Type 1

var a = new Array();

a[0] = 'dog';

a[1] = 'cat';

a[2] = 'hen';

a.length; // 3

// Type 2

var a = ['dog', 'cat', 'hen'];

a.length; // 3

// Looping through an array

for (var i = 0; i < a.length; i++) {

// Do something with a[i]

}

for (const currentValue of a) {

// Do something with currentValue

}

// Array methods

a.toString()

//Returns a string with the toString() of each element separated by commas.

a.join(sep)

//Converts the array to a string — with values delimited by the sep param

a.pop()

//Removes and returns the last item.

a.push(item1, ..., itemN)

//Appends items to the end of the array.

a.shift()

//Removes and returns the first item.

a.unshift(item1[, item2[, ...[, itemN]]])

//Prepends items to the start of the array.

a.slice(start[, end])

//Returns a sub-array.

a.sort([cmpfn])

//Takes an optional comparison function.

a.splice(start, delcount[, item1[, ...[, itemN]]])

//Lets you modify an array by deleting a section and replacing it with more items.

a.reverse()

//Reverses the array.

// Functions

function add(x, y) {

var total = x + y;

return total;

}

//functions have an inbuilt array of arguments

function add() {

var sum = 0;

for (var i = 0, j = arguments.length; i < j; i++) {

sum += arguments[i];

}

return sum;

}

add(2, 3, 4, 5); // 14

// more efficient way

function add(...args){

// do something with args array which can hold any number of arguments

}

// Anonymous function

var avg = function() {

var sum = 0;

for (var i = 0, j = arguments.length; i < j; i++) {

sum += arguments[i];

}

return sum / arguments.length;

};

// Creating class objects using constructor function notation

// note: this keyword

function makePerson(first, last) {

return {

this.first: first,

this.last: last,

fullName: function() {

return this.first + ' ' + this.last;

},

fullNameReversed: function() {

return this.last + ', ' + this.first;

}

};

}

var s = makePerson('Simon', 'Willison');

s.fullName(); // "Simon Willison"

s.fullNameReversed(); // "Willison, Simon"

// exporting modules

// type 1 - conventional

// module.exports is an global object

function hello(){

//do something

}

module.exports = {

hello

}

// type 2 - ecma script for node versions >14

// using keyword exports

// refer blog https://www.freecodecamp.org/news/node-module-exports-explained-with-javascript-export-function-examples/

Seite 3

Getting Started

Download nodejs from https://www.nodejs.org

Install an IDE

Hello World

create a file names helloWorld.js

add a line 'console.log('Hello world');' to the file

run the program using command '$ node helloWorld'

NPM

nodejs package manager

packages can be classified as global and local

Global packages are the packages installed in the system which can be accessed throughout the system

these modules are stored in a directory defined while installation

function as commandline tools as well

Local packages are the packages used for managing project dependencies

these modules are stored in node_modules directory within the project

can function as commandline tools using npx

$ npx local_package

npm commands

$ npm install express

$ npm install express@11.11.11

$ npm install -g express

install latest express module if version not mentioned else the given version locally

-g flag for global

$ npm install -i

install all the dependencies mentioned in the package.json

$ npm remove express

deletes an installed package

Informationen

Beschreibung
Simple Nodejs tutorials
Erstellt von
Omkar Gan · Jun 2021
Aktualisiert
22 Jun 2021
Aufrufe
13
Bewertung

Feedback

Worum geht es?

Wie gefällt dir das neue Design?

Dein Eindruck