0%

node.js

简单的说 Node.js 就是运行在服务端的 JavaScript且基于Chrome JavaScript 运行时建立的一个平台。
Node.js是一个事件驱动I/O服务端JavaScript环境,基于Google的V8引擎,V8引擎执行Javascript的速度非常快,性能非常好。

node.js 的安装(ubuntu)

nvm的安装

Modules

module.exports 和 exports

require只能看到的只有module.exports这个对象,它是看不到exports对象的,而我们在编写模块时用到的exports对象实际上只是对module.exports的引用

1.module.exports 初始值为一个空对象 {}
2.exports 是指向的 module.exports 的引用
3.require() 返回的是 module.exports 而不是 exports

示例1

1
2
3
4
# index.js
var content=require("./content");

content("nihao");
1
2
3
4
5
6
# content.js
function cont(cont){
console.log(“cont”+cont);
};

module.exports=cont;
1
2
# 输出结果
contnihao

示例2

1
2
3
4
# index.js
var content=require("./content");

content("nihao");
1
2
3
4
5
6
7
8
9
10
11
# content.js
function cont(cont){
console.log("cont"+cont);
};

function ent(ent){
console.log("ent"+ent);
};

module.exports=cont;
module.exports=ent;
1
2
#输出结果
entnihao

示例3

1
2
3
4
5
# index.js
var content=require("./content");

content.cont("nihao");
content.ent("nihao");
1
2
3
4
5
6
7
8
9
10
11
# content.js
function cont(cont){
console.log("cont"+cont);
};

function ent(ent){
console.log("ent"+ent);
};

exports.cont=cont;
exports.ent=ent;
1
2
3
# 输出结果
contnihao
entnihao

示例4

1
2
3
4
5
# index.js
var content=require("./content");

content.cont("nihao");
content.ent("nihao");
1
2
3
4
5
6
7
8
9
10
11
12
13
# content.js
function cont(cont){
console.log("cont"+cont);
};

function ent(ent){
console.log("ent"+ent);
};

module.exports={
cont:cont,
ent:ent
};
1
2
3
# 输出结果
contnihao
entnihao

require.main()

When a file is run directly from Node.js, require.main is set to its module. That means that you can determine whether a file has been run directly by testing

1
require.main === module

For a file foo.js, this will be true if run via node foo.js, but false if run by require('./foo').

Because module provides a filename property (normally equivalent to __filename), the entry point of the current application can be obtained by checking require.main.filename.

Process

process.argv()

Added in: v0.1.27
An array containing the command line arguments. The first element will be ‘node’, the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments.

1
2
3
4
// print process.argv
process.argv.forEach((val, index, array) => {
console.log(`${index}: ${val}`);
});

This will generate:

1
2
3
4
5
6
$ node process-2.js one two=three four
0: node
1: /Users/mjr/work/node/process-2.js
2: one
3: two=three
4: four

node.js 运行错误

Error: listen EADDRINUSE :::8888

错误提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
events.js:154
throw er; // Unhandled 'error' event
^

Error: listen EADDRINUSE :::8888
at Object.exports._errnoException (util.js:856:11)
at exports._exceptionWithHostPort (util.js:879:20)
at Server._listen2 (net.js:1227:14)
at listen (net.js:1263:10)
at Server.listen (net.js:1359:5)
at Object.start (/home/xuefeng/node/rumen6/server.js:24:32)
at Object.<anonymous> (/home/xuefeng/node/rumen6/index.js:10:8)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)

错误解决:

1
检查是否有占用8888端口的程序,尤其是查看是否已经有一个在运行的node实例在8888 端口上。