Mac Node JS

Node JS ?

NodeJS 是 js 的一个解析器,可以让 js 独立于浏览器执行。执行在浏览器中的 js 的解析器就是浏览器。

安装

Node js 下载地址,下载 apk ,双击安装.

终端输入:

1
2
3
4
$ which node
/usr/local/bin/node
$ node
>

表示安装成功。

运行

解析器模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ node
> 1 + 2
3
> console.log("hello node");
hello node
undefined
> function hello() {
... console.log("hello world");
... }
undefined
> hello()
hello world
undefined
>

xxx.js 文件方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# maple @ iMac in ~/workspace/NODEJS [11:12:53]
$ cat hello.js
function hello()
{
console.log("Hello world");

}


hello();

# maple @ iMac in ~/workspace/NODEJS [11:18:28]
$ node hello.js
Hello world

模块

Node JS 可以直接解析 JS code 文件并输出结果,可以将工程通过 JS 文件来进行划分,那么划分出来的各个模块之间的沟通便是一个问题。

例如,当前目录下两个文件 main.js & one.js

exports 导出模块函数

1
2
3
4
5
6
7

function hello() {
console.log("this is hello funciton");
return 1024;
}

exports.hello = hello;

require 读取模块函数

1
2
3
4
5
6

var one = require('./one');

console.log(one.hello());
console.log(one.hello());
console.log(one.hello());

运行

1
2
3
4
5
6
7
$ node main.js
this is hello funciton
1024
this is hello funciton
1024
this is hello funciton
1024
-------------本文结束谢谢欣赏-------------
Alice wechat