Node JavaScript

What is This

NodeJs

  • 利用前端javascript語言所開發後端語言
  • 擁有強大的模組擴充功能
  • 非同步機制與事件觸發
  • 適合全端開發者使用(前後端都使用javascript為開發)
  • 適合開發API Server
  • 具備NPM(NodeJs Package Manager)管理各種所需套件

 Install

Install NodeJS & npm

//Ubuntu
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs

On Ubuntu

Install NodeJS & npm

On Windows

Install Success

//確認nodejs版本號為4.4.0LTS
$ node -v 
v4.4.0

//確認NPM版本號為2.14.20以上
$ npm -v
2.14.20
Structe Introduction

變數的使用(var)

var variableInt = 0;
var variableStr = 'i am str';
var variableBoolean = true;

NodeJs 也是為弱型別的型態

Const 使用(定數)

const a = 1;
a = 2;

console.log(a);
//----------output----------
1

變數被宣告成const時,即無法進行變動

Function 

 

// one way
var func = function(input) {
    console.log(input);
};

func("just example");

//other way

function func(input) {
    console.log(input);
};

func("just example");

Class

function People() {
    this.name = 'no name';
    this.year = 0;
};

People.prototype.getName = function() {
    return this.name;
};

People.prototype.setName = function(name) {
    this.name = name;
};

People.prototype.getYear = function() {
    return this.year;
};

People.prototype.setYear = function(year) {
    this.year = year;
};

Nodejs 對於class比較沒其他程式語言這麼具體化


var people = new People;
people.setName('Vasiliy');
people.setYear(18);
console.log(people.getName());
console.log(people.getYear());
class People{
    public String name;
    public Integer year;

    public People() {
        this.name = 'no name'
        this.year = 0;
    }
    
    public void setName(name) {
        this.name = name;
    }
    
    public void setYear(year) {
        this.year = year;
    }
    
    public String getName() {
        return this.name;
    }
    
    public String getYear() {
        return this.year;
    }

}

Module

必須被實例出來的

//example.js
module.exports = Example;

function Example() {

};

Example.prototype.echo = function() {
	console.log('i am example');	
};
//app.js
var exampleModule = require('./example');
var example = new exampleModule();
console.log(example.echo());

Module

不需被實例出來的

//app.js
var example  = require('./example');
console.log(example.echo());
//example.js
module.exports = {
    echo: function() {
        console.log('i am example');
    }
};

NPM using

init

$ npm init 
........
//--------------------------------//
name: (123) example
//--------------------------------//
version: (1.0.0) 1.0.0
//--------------------------------//
description: just example
//--------------------------------//
entry point: (app.js)
//--------------------------------//
test command:
//--------------------------------//
keywords: nodejs,example
//-------------------------------//
author:vasiliy
//--------------------------------//
license: (ISC) MIT

build package.json

About to write to d:\CodingWorkSpace\NodeJs\123\package.json:          
                                                                       
{                                                                      
  "name": "example",                                                   
  "version": "1.0.0",                                                  
  "description": "just example",                                       
  "main": "app.js",                                                    
  "scripts": {                                                         
    "test": "echo \"Error: no test specified\" && exit 1"              
  },                                                                   
  "keywords": [                                                        
    "nodejs",                                                          
    "example"                                                          
  ],                                                                   
  "author": "vasiliy",                                                 
  "license": "MIT"                                                     
}                                                                      
                                                                                                                                          
Is this ok? (yes)                                                      

install package

$ npm install

安裝 package.json 所需具備的package

Example

install express

$ npm install express --save
var express = require('express');
var 路由器 = express();

路由器.get('/', function (請求, 回應) {
  回應.send('Hello World!');
});

路由器.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});
$ node app.js

Example For Web

look 127.0.0.1:3000

Pratice

製作一個9*9乘法表

$ node app.js

2*2=4   3*2=6   4*2=8   5*2=10  6*2=12  7*2=14  8*2=16  9*2=18
2*3=6   3*3=9   4*3=12  5*3=15  6*3=18  7*3=21  8*3=24  9*3=27
2*4=8   3*4=12  4*4=16  5*4=20  6*4=24  7*4=28  8*4=32  9*4=36
2*5=10  3*5=15  4*5=20  5*5=25  6*5=30  7*5=35  8*5=40  9*5=45
2*6=12  3*6=18  4*6=24  5*6=30  6*6=36  7*6=42  8*6=48  9*6=54
2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49  8*7=56  9*7=63
2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64  9*8=72
2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81

Answer

var str = '';
for(var i = 2;i < 10;i++) {
	for(var j = 2;j < 10;j++) {
		str = str + j + '*' + i + '=' + i*j + '\t';
	}
	str += '\n';
}

console.log(str);
Made with Slides.com