node07-http

 

目录:
node01-创建服务器
node02-util
node03-events
node04-buffer
node05-fs
node06-path
node07-http
node08-express
node09-cookie

 

 

http模块:

 

文件上传:

html:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>post</title>
 6     <link rel="stylesheet" href="post.css">
 7 </head>
 8 <body>
 9     <form action="/post" method="post" enctype="multipart/form-data">
10         用户名:<input type="text" name="user"><br>
11         密码:<input type="password" name="pass"><br>
12         <input type="file" name="fileup"><br>
13         <input type="submit" value="提交">
14     </form>
15     <div id="time">time</div>
16 </body>
17 <script src="post.js"></script>
18 </html>
View Code

 

js:

 1 window.onload = function(){
 2     var time = document.getElementById("time");
 3     var timer = setInterval(function(){
 4         var ajax = new XMLHttpRequest();
 5         ajax.open("get","/ajax?user=zhaoyang&pass=123",true);
 6         ajax.send();
 7         ajax.onload = function(){
 8             time.innerHTML = ajax.responseText;
 9         }
10     },1000);
11 
12 }
View Code

 

node(文件上传与get、post请求处理):

 1 /*
 2 * http:超文本传输协议
 3 * http模块:能够搭建服务器环境(模拟的客服端环境)
 4 * 
 5 * */
 6 
 7 var http = require("http");
 8 var url = require("url");
 9 var fs = require("fs");
10 var formidable = require("formidable");
11 var server = http.createServer(function (request,response) {
12     // console.log(request.url);
13     var urlObj = url.parse(request.url,true);
14     // console.log(urlObj);
15     // console.log(request.method);
16     // response.end("响应结束");
17 
18     if(urlObj.pathname == "/10post.html" || urlObj.pathname == "/"){
19         //访问表单页面
20         var rs = fs.createReadStream("10post.html");
21         rs.pipe(response);
22         // response.end();不能带end()
23     }else if(urlObj.pathname == "/post"){
24         // console.log(urlObj);
25     //     request.on("data",function(chunk){
26     //         console.log(chunk);
27     //     });
28 
29         var form = new formidable.IncomingForm();
30         form.parse(request,function (err,fields,files) {
31             // console.log(fields);
32             console.log(files);
33             console.log(files.fileup.path);//文件上传的临时路径 files.fileup.path
34             fs.createReadStream(files.fileup.path).pipe(fs.createWriteStream("./upload/" + files.fileup.name));
35 
36         });
37         response.end();
38     }else if(urlObj.pathname == "/ajax"){
39         var newTime = new Date();
40         response.end(newTime.toString());
41     }else if(urlObj.pathname == "/req"){
42         console.log(urlObj.query);
43         var result= "";
44         request.on("data",function(chunk){
45             result += chunk.toString();
46         });
47         request.on("end",function(err){
48             response.end("参数是" + result);
49         })
50         response.end("参数是" + JSON.stringify(urlObj.query));
51     }else if(urlObj.pathname != "/favicon.ico"){
52         //其他的所有页面
53         // ./post.css
54         console.log(urlObj.pathname);
55         fs.createReadStream("." + urlObj.pathname).pipe(response);
56     }
57 });
58 server.listen(2333);
View Code

 

GET、POST请求:

 1 var http = require("http");
 2 var request = http.request({
 3     host:"localhost",
 4     port:"2333",
 5     path:"/req?user=zhaoyang&pass=123",
 6     // path:"/req",
 7     method:"POST",
 8 },function (res) {
 9     console.log(res.statusCode);
10     var result = "";
11     res.on("data",function(chunk){
12         result += chunk;
13     });
14     res.on("end",function(err){
15         console.log(result);
16     });
17 });
18 request.write('{"user":"zhaoyang","pass":"123"}');
19 request.end();//结束请求体
View Code

 

posted @ 2016-12-16 20:17  prolog  阅读(214)  评论(0编辑  收藏  举报