Node.
js Tutorial
Table of Contents
[Link] Tutorial.................................................................................................................1
What is [Link]?........................................................................................................................1
What is a Module in [Link]?.....................................................................................................1
Include build-in Modules............................................................................................................1
Access Modules..........................................................................................................................1
Create own Modules..................................................................................................................1
Include own Module..................................................................................................................1
Install Module............................................................................................................................1
Package......................................................................................................................................1
What is [Link]?................................................................................................................2
Create [Link]...................................................................................................................2
Process.......................................................................................................................................2
Lightship....................................................................................................................................2
Using dotenv package to create environment variables..............................................................2
Ref:............................................................................................................................................2
What is [Link]?
Is Open source server environment
Allow run javascript on Server
Is single-threaded, non-blocking, async program
What is a Module in [Link]?
Like javascript library
A set of functions include in your application
[Link] module is a type of package that can publish to npm
Include build-in Modules
var http = require(‘http’);
Access Modules
[Link](function (req, res) {
[Link](200, {'Content-Type': 'text/html'});
[Link]('Hello World!');
}).listen(8080);
Create own Modules
Create own module, and easily include in your application
Use exports keyword to make properties and method available outside the module
file
[Link] = function () {
return Date();
};
Save to [Link]
Include own Module
var dt = require('./myfirstmodule');
Install Module
Install module that all application can use
npm install -g module_name
Package
A package is file / directory which is package or modules in package.
A package must have [Link] file
What is [Link]?
Is the manifest file of [Link]
A module can reference by other application
Create [Link]
In package folder, cd /path/to/package
Run “npm init –scope=@scope-name”
Process
An object is a global, global is always available to [Link]
No need const process = require(‘process’);
Process object is an instance of EventEmitter
E.g. Event: ‘beforeExit’, the ‘beforeExit’ event is emitted (發射) when [Link] empties
the event loop and has no additional work to schedule.
Lightship
Check readiness / liveness of [Link] in Kubernetes
Using dotenv package to create environment variables
1. Install dotenv
a. In the app folder
b. Npm install dotenv –save
c. It will create the node_module folder and [Link]
2. Add a line to the app
a. Require(‘dotenv’).config();
3. Create a .env file at the root directory of app folder and add variable to it
a. SECRET_KEY=abcd1234
[Link], require
1. [Link]
a. Var text = “hello”;
b. [Link] = text; // like return
2. [Link]
a. Var example = require(‘./[Link]’); // call function
b. [Link](example); // Print hello
1. [Link]
a. Var text = “hello”;
b. [Link] = {
getText: function() {
return text;
}
};
2. [Link]
a. Var example2 = require(‘[Link]’);
b. [Link]([Link]());
[Link] [Link] = object, Like return
[Link] = function,
[Link] = string
Require(‘’) Call function
EventEmitter
Different between [Link]() and [Link]()
[Link]() Full event listener
[Link]() One time listener, will detach after firing
once
Ref:
W3schools [Link] Tutorial [Link]
Guru99 [Link] Tutorial [Link]
[Link]
Using dotenv package to create environment [Link]
variables dotenv-package-to-create-environment-
variables-33da4ac4ea8f
Nodejs website [Link]
Nodejs guides [Link]
NPM website [Link]
Dockerizing a [Link] web-app [Link]
docker-webapp/
Javascript tutorial [Link]
[Link] module export [Link]
module-exports-exports-node-js/
Javascript Promise [Link]
promise/
EventEmitter example [Link]
[Link]