zoukankan      html  css  js  c++  java
  • Shell 简单构建 Node web服务器

    .git bash 执行代码生成:

    ./makeJs.sh

    生成文件如下:

    访问:http://127.0.0.1:3030/index.html

    makeJs.sh  代码如下:

    #create makePost.sh
    
    post=./makePost.sh
    if [ -f "$post" ];then
    	echo "$post 文件己存在"
    else
    (
    cat <<EOF
    count=1
    while [ "$#" -ge "1" ];do
        echo "参数序号为 $count 是 $1"
    	curl -i -X POST -H 'Content-type':'application/json' -d {"BTime":""$btime""} $1
    
        let count=count+1
        shift
    done
    EOF
    ) > $post
    fi
    
    #create index.html
    index=./index.html
    if [ -f "$index" ];then
    	echo "$index 文件己存在"
    else
    (
    cat <<EOF
      testing
    EOF
    ) > $index
    fi
    
    #create index.js
    file=./index.js
    if [ -f "$file" ];then
    echo "文件己存在!无法生成 $file"
    else
    (
    cat <<EOF
    var http=require('http');
    var fs = require('fs');
    var url = require('url');
     
    http.createServer(function(request,response) {
        var pathname= url.parse(request.url).pathname;
        console.log("Request for "+ pathname + "  received.");
        fs.readFile(pathname.substr(1),function(err, data) {
            if(err) {
                response.writeHead(404,{'Content-Type': 'text/html'});
            }
            else {
                response.writeHead(200,{'Content-Type': 'text/html'});
                response.write(data.toString());
            }
            response.end();
        });
    }).listen(3030);
     
    console.log('Server running at http://127.0.0.1:3030/index.html');
    EOF
    ) > $file
    
    echo "文件创建成功"
    node index.js
    
    fi
    

      

    makeJs.sh 解决服务资源CSS/JS/other  文件载入:(推荐)

    // --------
    #create makePost.sh
    
    post=./makePost.sh
    if [ -f "$post" ];then
    echo "$post 文件己存在"
    else
    (
    cat <<EOF
    count=1
    while [ "$#" -ge "1" ];do
        echo "参数序号为 $count 是 $1"
    curl -i -X POST -H 'Content-type':'application/json' -d {"BTime":""$btime""} $1
    
    let count=count+1
    shift
    done
    EOF
    ) > $post
    fi
    
    #create index.html
    index=./index.html
    if [ -f "$index" ];then
    echo "$index 文件己存在"
    else
    (
    cat <<EOF
    testing
    EOF
    ) > $index
    fi
    
    #create index.js
    file=./index.js
    if [ -f "$file" ];then
    echo "文件己存在!无法生成 $file"
    else
    (
    cat <<EOF
    
    var http = require('http');
    var fs = require('fs');
    
    //主要思想就是任何一个静态文件也应该做响应,一个获取静态文件都应当请求来处理,这是主要思想
    var server = http.createServer();
    
    var handlerequest = function(request,respone){
        var url = request.url,ns;
        // respone.writeHead(200,{'Content-Type':'text/html'});
        if(url=='/'){
            respone.writeHead(200,{'Content-Type':'text/html'});
            ns = fs.readFile('./index.html', function(err, data) {
                if (err) {
                    console.error(err);
                    return;
                }
                respone.end(data);
            });
            //respone.end();
        }else if(url != '/'){
            var surl = '.'+url;
            var type = surl.substr(surl.lastIndexOf(".")+1,surl.length);
            respone.writeHead(200,{'Content-type':"text/"+type});
            // respone.writeHead(200,{'Content-Type':'text/css'});
            ns = fs.readFile(surl, function(err, data) {
                if (err) {
                    console.error(err);
                    return;
                }
                respone.end(data);
            });
        }
    };
    server.on('request',handlerequest);
    
    server.listen(3030,function(){
        console.log('Server running at http://127.0.0.1:3030/index.html');
    });
    
    EOF
    ) > $file
    
    echo "文件创建成功"
    node index.js
    
    fi

    Post 接口批量测试

    ./makePost.sh http://192.168.3.52/ad/customer/res
    

      使用 curl 测试post请求json接口
       

    ./makePost.sh
    count=1
    while [ $# -ge "1" ];do
    echo "参数序号为 $count 是"
    curl -i -X POST -H 'Content-type':'application/json' -d {"BTime":""$btime""} $1
    let count=count+1
    shift
    done
    

      

  • 相关阅读:
    J2EE规范标准
    怎样用Google APIs和Google的应用系统进行集成(4)----获得Access Token以通过一些Google APIs的OAuth2认证
    [BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)
    大数据存储之分布式文件系统(一)
    List与array的相互转换
    TRIZ系列-创新原理-31-多孔材料原理
    关于app.FragmentManager和v4包的FragmentPagerAdapter冲突
    POJ 1163 The Triangle
    ChinaVis2015 第一天会议
    JSON初入门
  • 原文地址:https://www.cnblogs.com/q1104460935/p/10028859.html
Copyright © 2011-2022 走看看