在线解压rar网站/关键词在线听
javaScript 前台语言
nodejs 语法基于js 后台 重点 模块的学习 expressNode.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。 Node.js 使用了一个事件驱动、非阻塞式 I/O 的模型,使其轻量又高效。 Node.js 的包管理器 npm,是全球最大的开源库生态系统。
后台那么多为什么还要node.js
1. 异步事件驱动2. 非堵塞I/OCPU密集型(CPU-bound)IO密集型(I/O bound)性能出众
入门
入门案例一
console.log('asdfsd');
入门案例二
var oDate=new Date();console.log(oDate.getFullYear());
入门案例三
var re=/\d+/g;console.log("sdf343 34534ree r5345345g 345".match(re));
http模块介绍
http简介超文本传输协议 网络数据传输必须遵循的协议
监听
所谓的端口监听,是指主机网络进程接受到IP数据包后,察看其的目标端口是不是自己的端口号,如果是的话就接受该数据包进行处理。
案例一
const http=require('http');var server=http.createServer(function (request, response){console.log('有人来了');
});//监听——等着
//端口-数字
server.listen(8080);
案例二
请求与响应
request 请求 输入-请求的信息
response 响应 输出-输出的东西const http=require('http');var server=http.createServer(function (req, res){//console.log('有人来了');res.write("abc");res.end();
});//监听——等着
//端口-数字
server.listen(8080);//http://localhost:8080/
案例三
const http=require('http');var server=http.createServer(function (req, res){switch(req.url){case '/1.html':res.write("111111");break;case '/2.html':res.write("2222");break;default:res.write('404');break;}res.end();
});//监听——等着
//端口-数字
server.listen(8080);//http://localhost:8080/
文件操作
阻塞读取文件
var fs = require("fs");var data = fs.readFileSync('input.txt');console.log(data.toString());
console.log("程序执行结束!");
node.js的文件操作是异步的异步与同步介绍
request 请求 输入-请求的信息
response 响应 输出-输出的东西
Node.js 回调函数
Node.js 异步编程的直接体现就是回调。异步编程依托于回调来实现回调函数在完成任务后就会被调用,Node 使用了大量的回调函数,Node 所有 API 都支持回调函数。例如,我们可以一边读取文件,一边执行其他命令,在文件读取完成后,我们将文件内容作为回调函数的参数返回。这样在执行代码时就没有阻塞或等待文件 I/O 操作。这就大大提高了 Node.js 的性能,可以处理大量的并发请求。回调函数一般作为参数的最后一个参数出现:
http与fs整合
const http=require('http');
const fs=require('fs');var server=http.createServer(function (req, res){//req.url=>'/index.html'//读取=>'./www/index.html'// './www'+req.urlvar file_name='./www'+req.url;fs.readFile(file_name, function (err, data){if(err){res.write('404');}else{res.write(data);}res.end();});
});server.listen(8081);
node.js接受前台参数
手动自己接受
const http=require('http');http.createServer(function (req, res){var GET={};if(req.url.indexOf('?')!=-1){var arr=req.url.split('?');//arr[0]=>地址 '/aaa'var url=arr[0];//arr[1]=>数据 'user=blue&pass=123456'var arr2=arr[1].split('&');//arr2=>['user=blue', 'pass=123456']for(var i=0;i<arr2.length;i++){var arr3=arr2[i].split('=');//arr3[0]=>名字 'user'//arr3[1]=>数据 'blue'GET[arr3[0]]=arr3[1];}}else{var url=req.url;}console.log(url, GET);//req获取前台请求数据res.write('aaa');res.end();
}).listen(8080);
通过queryStirng模块接受
queryStirng演示
const querystring=require('querystring');var json=querystring.parse("user=blue&pass=123456&age=18");
console.log(json);
const http=require('http');
const querystring=require('querystring');http.createServer(function (req, res){var GET={};if(req.url.indexOf('?')!=-1){var arr=req.url.split('?');var url=arr[0];GET=querystring.parse(arr[1]);}else{var url=req.url;}console.log(url, GET);//req获取前台请求数据res.write('aaa');res.end();
}).listen(8080);
通过url模块接受
url案例
const urlLib=require('url');var obj=urlLib.parse("http://www.zhinengshe.com/index?a=12&b=5", true);console.log(obj.pathname, obj.query);
const http=require('http');
const urlLib=require('url');http.createServer(function (req, res){给true就自动将query部分转成jsonvar obj=urlLib.parse(req.url, true);var url=obj.pathname;var GET=obj.query;console.log(url, GET);//req获取前台请求数据res.write('aaa');res.end();
}).listen(8081);
post参数的接受
const http=require('http');
const querystring=require('querystring');http.createServer(function (req, res){//POST——reqvar str=''; //接收数据//data——有一段数据到达(很多次)var i=0;req.on('data', function (data){console.log(`第${i++}次收到数据`);str+=data;});//end——数据全部到达(一次)req.on('end', function (){var POST=querystring.parse(str);console.log(POST);});
}).listen(8080);
post与readFile整合
const http=require('http');
const fs=require('fs');
const querystring=require('querystring');
const urlLib=require('url');var server=http.createServer(function (req, res){//GETvar obj=urlLib.parse(req.url, true);var url=obj.pathname;const GET=obj.query;//POSTvar str='';req.on('data', function (data){str+=data;});req.on('end', function (){const POST=querystring.parse(str);/*url——要什么GET——get数据POST——post数据*///文件请求var file_name='./www'+url;fs.readFile(file_name, function (err, data){if(err){res.write('404');}else{res.write(data);}res.end();});});
});server.listen(8081);
转载于:https://blog.51cto.com/13477015/2310579