js:nodejs简单的Http服务器搭建

一,用过一个nodejs平台,搭建简单的Http服务器

Node.js不是一种独立的语言,也不是javaScript框架,Node.js是一个让javaScript运行在服务端的开发平台,node.js

也是通过javascript语言编写。

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

var server=http.createServer(function(req,res){
        //向控制台输出测试是否接收到请求
        console.log("sdsadadadda");
        //接收url传递的参数
        var filePath=url.parse(req.url).pathname;
        //判断接收的参数中是否有page
        if(filePath.indexOf("page"!=-1)){
            //为即将加载的文件设置类型
            res.writeHead(200,{'Content':'text/html;charset=utf-8'});
            outputfile(res,filePath);
        }
        if(filePath.indexOf("img")!=-1){
            var content= fs.readFileSync("./"+filePath,"binary");
            res.write(content,"binary");
            res.end();
        }
        if(filePath.indexOf("css"!=-1)){
            res.writeHead(200,{'content':'text/css;charset=utf-8'});
            outputfile(res,filePath);
        }
        if(filePath.indexOf("js"!=-1)){
            res.writeHead(200,{'content':'text/js;charset=utf-8'});
            outputfile(res,filePath);
        }
    })

//通过该路径寻找页面
function outputfile(res,filePath){
    //寻找页面
    fs.readFile("Users/sb/WebstormProjects/First/"+filePath,'utf-8',function(err,data){
        if(err){
            //未找到返回错误信息
            res.write("错误提示:"+err);
            res.end();
        }else{
            //找到后显示该页面
            res.write(data);
        //关闭通道 res.end(); } }) }
//http设置端口 server.listen(3030);

这样通过访问本机的ip地址端口号Users/sb/WebstormProjects/First/可以访问此文件夹下的网页

 

一,获取请求参数

当前url   http://localhost:8888/select?aa=001&bb=002

var http = require('http');

var URL = require('url');

http.createServer(function(req, res){

   var arg = url.parse(req.url).query;  //方法一arg => aa=001&bb=002

   var arg = url.parse(req.url, true).query;  //方法二arg => { aa: '001', bb: '002' }

   console.log(arg.aa);//返回001

   console.log(arg.bb);//返回002

   //然后就可以根据所得到的数据处理了

}).listen(8888);//建立服务器并监听端口

 

url.parse(urlStr, [parseQueryString], [slashesDenoteHost])

接收参数:

urlStr       url字符串

parseQueryString    true时参数以json格式显示,默认为false

slashesDenoteHost   默认为false//foo/bar 形式的字符串将被解释成 { pathname: //foo/bar' }

如果设置成true//foo/bar形式的字符串将被解释成  { host:foo', pathname: /bar' }

 

 

二,获取特定url参数值

var testUrl =  'http://localhost:8888/select?aa=001&bb=002';

var p = URL.parse(testUrl);

console.log(p.href); //取到的值是:http://localhost:8888/select?aa=001&bb=002

console.log(p.protocol); //取到的值是:http:

console.log( p.hostname);//取到的值是:192.168.50.137;

console.log(p.host);//取到的值是:localhost:8888

console.log(p.port);//取到的值是:8888

console.log(p.path);//取到的值是:/select?aa=001&bb=002

console.log(p.hash);//取到的值是:null

console.log(p.query);// 取到的值是:aa=001

在此值得注意的是当语句 var p = URL.parse(testUrl, true) ,p则返回的是如:{aa:'001'}这样的对象, 直接打印p.query则返回 [object Object],这时我们可以这样 写: co.querynsole.log(p.query.aa); //取到的值是:001

console.log( p.pathname);//取到的值是:/select

req.params.xxxxx path中的变量

req.query.xxxxx get中的?xxxx=

req.body.xxxxx post中的变量

 

服务器的响应(send write render)

res.write()

   页面通过ajax请求服务器,服务器通过此方法返回的是字符串的格式。

res.send()

页面通过ajax请求服务器,服务器通过此方法返可直接返回对象。

: mongodb中要使用res.send()返回数据而不是res.write()

res.render(arg1,arg2)

    主要用于在模版中返回数据到页面,第一个参数是页面路径(通过配置省略了后辍)

    第二个参数是返回模版页面的参数。

res.render('test',{"args":{name:"测试名称 ", list:["abc","def","ggg"]}  });

  发送了name 的字符串参数,与list的对象参数。

 res.redirect(url)

    主要用于重定向页面。

posted @ 2017-12-20 21:48  dybe  阅读(397)  评论(0编辑  收藏  举报