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

  

 

posted @ 2018-11-27 20:36  徐锅  阅读(369)  评论(0编辑  收藏  举报