node.js--初步入门使用

1.Hello World

  var http=require('http');
  //创建服务器
  var server=http.createServer(function(req,res){
  //requsert:请求内容
  //response:响应内容
  console.log('hello world');
  //node平台提供的方法
  res.writeHead(200,{"Content-type":"text/html;charset=utf-8"})
  //设置请求头信息
  res.end('结束');
  //结束。响应,一定要加,要不会一直响应

  })

  //监听

  server.listen(8888,'127.0.0.1');

2.读取文件

  var http=require('http');
  //引入http模块
  var fs=require('fs');
  var server=http.createServer(function(req,res){
  //获取路径
  var path=req.url;

  console.log(path);
  //node没有web容器的概念
  if(path=='/the'){
      //读取文件fs.readFile(请求路径,回调函数))
      fs.readFile(path,function(err,data){
      data='成功了';
      res.writeHead(200,{"Content-type":"text/html;charset=utf-8"})
      res.end(data);
      })
    }else{
      res.writeHead(404,{"Content-type":"text/html;charset=utf-8"})
      res.end("请输入网站");
    }


  })

    server.listen(8888,'127.0.0.1');

3 GET和POST表单提交方式

  --html文件

  

  <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8">
        <title>get和post方式</title>
      </head>

      <body>

          //method:两种提交方式:get和post
        <form action="http://127.0.0.1:8888/" method="post">
          用户名:<input type="text" name="username" /></br></br>
          密&nbsp;&nbsp;码:<input type="password" name="passwd"/></br></br>
          性别:<input type="radio" value="女" name="sex"/>女<input type="radio" value="男 name="sex"/> 男</br></br>
          爱好:<input type="checkbox" name="hobby" value="play" />玩
             <input type="checkbox" name="hobby" value="eat"/> 吃
             <input type="checkbox" name="hobby" value="sleep"/>睡<br><br>
          <input type="submit" value="提交"/>
        </form>
      </body>
    </html>

  --get.js

  

    var http=require('http');
    var url=require('url');

    var server=http.createServer(function(req,res){

      if(req.url=='/favicon.ico'){
        return
      }
      //url.parse解析url 解析为对象的形式 pathname query path
      //url.parse(url,true) 把查询参数query转化为对象
      console.log(typeof url.parse(req.url,true).query);
      console.log(url.parse(req.url,true).query);
      var info=url.parse(req.url,true).query;

      console.log(info);
      console.log(typeof info);

      res.writeHead(200,{"Content-type":"text/html;charset=utf-8"});
      res.end("用户的姓名:"+info.username);
  }).listen(8888,'127.0.0.1')

  --post.js

  

    var http=require('http');
    var querystring=require('querystring');

    http.createServer(function(req,res){

      if(req.url=='/favicon.ico'){
        return
      }

      var all='';
      //异步请求
      //post请求,数据分段请求。每一个数据块就是一个chunk。加载数据块
      req.addListener('data',function(chunk){

        console.log(chunk);
        all+=chunk;
      })
      //post数据请求接收完毕。end检测是否接收完毕
      //querystring.parse(a) 把a转化为对象
      req.addListener('end',function(chunk){
        console.log(querystring.parse(all));
      })

     console.log(typeof querystring.parse(all))

       res.end();
    }).listen(8888,'127.0.0.1');

猜你喜欢

转载自www.cnblogs.com/Lshiyun/p/9685932.html