nodejs(1)

时间:2021-05-01 22:49:30

node.js 是一个让javascript运行在服务端的开发平台

node.js的环境部署

1.下载安装包

https://nodejs.org/en/

安装后 打开cmd的dos窗口

运行node

console.log('success')nodejs(1)

编写一个demo.js并保存到nodejs的安装目录里

consloe.log('学习nodejs')

nodejs(1)

在dos里运行demo.js 可以看到输出的学习nodejs

nodejs(1)

node.js命令行工具

node -v 查看版本

node -e ‘console.log("hello word")’;  直接运行

node  直接进入编译模式

console.log('1111')

第一行是输入、第二行是返回值

nodejs(1)

建立HTTP服务器

 var http = require('http');
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text/html'});
res.write('<h1>nodejs</h1>');
res.end('<p>视频PCAT</p>');
}).listen(2000);

保存为demo2.js并用cmd来运行

nodejs(1)

在浏览器中输入:localhost:2000

nodejs(1)

回调函数

1.异步式读取文件

var fs = require('fs');
fs.readFile('file.txt','utf-8',function(err,data){
if(err){
console.log(err);
}else{
console.log(data)
}
});
console.log('end.');
结果:
end.
Contents of the file.

在nodejs目录里编写一个file.js

 var fs = require('fs');
fs.readFile('file.txt','utf-8',function(err,data){
if(err){
console.log(err);
}else{
console.log(data)
}
});
console.log('end.');

在nodejs目录里创建一个file.txt文件并写入hi,i'm a file.txt

打开cmd 执行file.js

nodejs(1)

同步式读取文件

var fs = require('fs');
var data=fs.readFileSync('file.txt','utf-8');
console.log(data);
console.log('end.');

显示如下

nodejs(1)

事件

1.普通事件的对象  (在nodejs目录里创建一个event.js)

//声明事件对象
var EventEmitter = require('events').EventEmitter;
var event = new EventEmitter();
//注册事件
event.on('some_event',function(){
console.log('这是一个自定义的事件');
})
//在1s时间之后触发事件
setInterval(function(){
event.emit('some_event');
},1000);

显示结果如下:

nodejs(1)

node.js的事件循环机制

node.js程序是由事件循环开始,到事件循环结束,所有的逻辑都是事件的回调函数。

如何使用自定义事件呢

事件的回调函数在执行的过程中,可能会发出IO请求或直接发射(emit)事件,这I型完毕后再返回事件循环。

node.js中的模块和包的应用:

1.在nodejs安装目录中创建一个模块 model.js

var  name;
exports.setName=function(theName){
name = theName;
}
exports.sayHello=function(){
console.log('hello'+name);
}

2.在nodejs安装目录中创建一个模块 getModel.js

var myModel = require('./model');
myModel.setName('hehe');
myModel.sayHello();

nodejs(1)

node.js中require不能重复加载模块 只能包含一次

我们把一个对象封装到模块中引入模块 来实现多次调用

例:在node的安装目录中创建一个Sing.js

function hello(){
var name;
this.setName = function(theName){
name = theName;
}
this.sayHello = function(){
console.log('hello'+name);
}
}
module.exports=hello;

在node的安装目录中创建一个getSing.js

var hello = require('./Sing');
var he = new hello();
he.setName('hhehe');
he.sayHello();
var he1 = new hello();
he1.setName('aaaa');
he1.sayHello();

nodejs(1)

包的创建

创建一个package包

1. package.json必须在包的顶层目录下

package.json里面代码

{
"main" : "./lib/index.js"
}

2.创建bin文件夹  里面放二进制文件

3.创建doc文件夹  里面放文档

4.创建test文件夹   里面放测试的

5.创建lib文件夹  放一个index.js

index.js里面代码:

exports.sayHello = function(){
console.log('this is a easy package');
}

具体package文件夹下的所有文件

nodejs(1)

在node安装目录里创建一个getPackage.js

getPackage.js里面的代码:

var pac = require('./package');
pac.sayHello();

在cmd下测试如下:

nodejs(1)

node.js在调用某个包的时候。会检查包中的package.json文件中的main字段,将其作为包的 接口模块,如果package.json或main字段不存在,会尝试寻找index.js或index.code作为包的接口。

package.json的规范属性

nodejs(1)

如何用包管理器

1.获取一个包

npm [install/i] [package_name]

例如:安装express包

npm i express

卸载包

npm uninstall 报名 【-g】

查看当前报名

npm list

帮助链接:http://www.cnblogs.com/bluefrog/archive/2012/08/14/2639085.html

2.本地模式和全局模式

npm在默认情况下会从http://npmjs.org搜索或下载包 将包安装到npm_modules

a.默认是npm install包名 作为本地模式

b.全局模式

npm install -g 包名

nodejs(1)

用cmd创建包里里面的package.json

nodejs(1)

安装包

nodejs(1)

查看

nodejs(1)

测试

nodejs(1)

nodejs(1)