node.js入门
2017-11-30 20:22:03
全局对象(global)
__dirname //文件所在目录/路径 C:/wamp/www/nodejs
__filename //文件的绝对路径 C://wamp/www/nodejs/test.js
console.log();打印成日志
console.info();//输出一条信息20:22:01
console.error();//打印错误
console.warn();//输出警告
console.time();//时间测试方法开始 //统计一段代码的执行时间
测试中间执行程序所耗费的时间
console.timeEnd();//时间测试方法结束
例子:
console.time('test');
for(var i=0;i<1000000;i++){}
console.timeEnd('test');
process.stdout //标准的信息输出
process.stdout.write('this is stdout');
process.stderr //标准的错误输出
process.stdout.write('this is stdout');
process.stdin.setEncoding('utf-8');//设置文本字符集
process.stdin.on('data',function(data){ //如何读取用户的键盘输入
console.log(data);
}) ;
process.stdin.on('radable',function(){ //有东西可读取的时候就会出这个事件 readble 可以提取用户输入的内容
var data = process.stdin.read();
console.log(data);
});
console.log(process.cwd());//打印当前工作目录
process.on//如何通过process.on操作系统对node发出的信号进行监听事件
exit //事件 程序正常退出时所触发的事件
例子:
process.on('exit',function(){
console.log('programe will exit.');
})
SIGINT //当有一个中断的信号出发的时候就会出发SIGINT事件 它会改变程序推出的默认行为
例子:
process.on('SIGINT', function(){
process.log('programe will SIGINT');//这里可以写程序退出前执行的程序
process.exit();//退出的方法
})
process.argv //可以封装我们在cmd终端输入的用空格分开的内容
例子:
node test.js zhangsan age sex tel ...........
第一项内容是node命令所在的目录路径
第二性内容是文件所在目录的路径
之后的内容就是执行文件时后面用空格隔开的附加内容
node test.js 1>log.txt 2>err.txt //将打印内容输出到文件1 将错误输出到文件2
node test.js 1>log.txt 2>&1 //将文件输出的内容正常信息和错误信息都输出到文件一中
模块和包
模块:一个实现某些特定功能的文件,以实现模块化编程
--通过require(路径/模块名);引入模块
--模块中的功能(如:变量/函数)通过赋给exports对象的某个属性供给调用者使用
例子:
执行文件内容
var sum = require('./sum');//通过require引入模块,并将对象赋值给sum对象
console.log(sum.sum(1000));//打印输出sum对象中的sum方法
模块文件内容
function(n){
var sum = 0;
for(var i = 1; i<=n; i++){
sum += i;
}
return sum;
}
exports.sum = sum; //exports 是将sum这个方法暴露出去 让外面的文件可以调用实例化sum这个方法.如果不使用exports将文件内容暴露出去,那么其他文件将无法引用该文件
包:包就是一个文件夹,他将模块分装起来,用于发布/更新/依赖管理和版本控制
--通过package.json来描述包的信息:入口文件,依赖的外部包等等
--通过npm install 命令来安装包,并通过require使用包
例子:
npm install cookie //通过npm install命令安装cookie包
npm remove cookie //删除包
node中的javascript
文件相关操作
--fs//内置文件操作模块
repuire('fs')
fs.readFile(filepath,function(err,data){})
例子:
//这个为异步执行的的方式
fs.readFile('sum.js','utf-8', function(err, data){//黑色标注的两个地方是使文件输出的内容为正常文本文件,二者可选则其一.如果不用这两个方法.输出的则是ASCE码
if(err){//判断文件是否有错误,如果有错误就输出
console.log(err);
}
else{//否则没有错误就输出数据
console.log(data.toString());//输出文件的文本内容
}
});
var data = fs.readFileSync('sum.js', 'utf-8');//同样可以接收文件内容//这个为同步执行的代码
--path
require('path')
path.sep //通过console.log(path.sep)就可以知道系统的分隔符采用的是什么样的斜杠
path.extname() //通过console.log(path.extname('test.txt'));打印文件扩展名
node http server服务器创建
require('http')
--var server = http.createServer(cb);
--server.listen(port);
例子:
var http = require('http');
var fs = require('fs');
var server = http.createServer(function(request, response){
fs.readFile('index.html','utf-8', function(err,data){//读取文件
response.end(data);//将文件内容返回到浏览器页面
});
//response.end('<html><head><meta charset="utf-8"></head><h1>it work</h1></html>');//将test.cn返回到浏览器页面显示出来
});
server.listen(8080)//监听端口号和IP地址
require('url')//引入url模块
--parse//函数就是解析url字符串的
例子:
var http = require('http');
var url = require('url');
http.createServer(function(request, response){
var urlObj = url.parse(request.url);
console.log(urlObj);
response.end('url parse');
}).listen(8080);
//终端打印内容
Administrator@WXVP3CE8E4OZ0EN MINGW64 /c/wamp/www/nodejs (master)
$ node test.js
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '?id=abc', //这里的内容是链接地址后面加的参数 如:http://localhost:8080/?id=abc
query: 'id=abc', //这里的内容是请求的参数和参数的值id=abc
pathname: '/', //请求的具体的资源/文件
path: '/?id=abc', //包含了参数的的资源
href: '/?id=abc' }
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: null,
pathname: '/favicon.ico',
path: '/favicon.ico',
href: '/favicon.ico' }
例子:
var http = require('http');
var url = require('url');
var fs = require('fs');
http.createServer(function(request, response){
var urlObj = url.parse(request.url);
console.log(urlObj);
var pathname = urlObj.pathname;
var query = urlObj.query;
if(pathname === '/'){
readFileAndResponse('/index.html', response);
}
else if(pathname === '/ajax'){
response.end('{#msg#:#this is a json response.#}');
}
else{
readFileAndResponse(pathname, response);
}
//response.end('url parse');
}).listen(8080);
function readFileAndResponse(pathname,response){
fs.readFile(pathnamesubstr(1),'utf-8',function(err,data){
if(err){
//console.log(err);//访问出错时输出这条语句
response.writeHead(404); //如果错误代码是404则输出下面语句
response.end('file not found.');//出现404错误时输出的语句
}
else{
response.end(data);//请求成功时返回的内容
}
})
}