Node.js系列02

1.Node.js 中 moudle.exprots 的用法:

如果暴露一个类, 就用module.exprots 。

 1 /**
 2  * Created by aytsoft on 2016/4/25.
 3  */
 4 /*如果是暴露一个类 就用moudle.exprots = 类名*/
 5 function Pepole(name, age){
 6     this.name = name;
 7     this.age = age;
 8 
 9 }
10 Pepole.prototype = {
11     constructor:Pepole,
12     getMsg :function(){
13         console.log(this.name + "  "+ this.age);
14     }
15 }
16 module.exports = Pepole;

2. querystring 的简单用法:

post提交

 1 /**
 2  * Created by aytsoft on 2016/4/25.
 3  */
 4 var http  = require("http");
 5 var queryString = require("querystring");
 6 
 7 
 8 var server = http.createServer(function(req, res){
 9     if(req.url =="/favicon.ico"){
10         return;
11     }
12    if(req.url=="/userData" && req.method.toLowerCase()=="post"){
13        var maindata;
14        req.addListener("data", function(chunk){
15            maindata += chunk;
16            console.log(chunk);
17        });
18        req.addListener("end", function(){
19            var obj = queryString.parse(maindata)
20            console.log(obj);
21            console.log(obj.name);
22            console.log(obj.sex);
23            res.end("hello");
24 
25        });
26 
27    }
28 }).listen(3000,"127.0.0.1");

html 页面

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8    <form method="post" action="http://127.0.0.1:3000/userData" >
 9     name:
10        <input type="text" name="name">
11        <br>
12     sex:
13        <input type="radio" name="sex" value="man">man
14        <input type="radio" name="sex" value="female">female
15        <br/>
16     <input type="submit" value="submit">
17 
18    </form>
19 </body>
20 </html>
原文地址:https://www.cnblogs.com/aytsoft/p/5514236.html