Shell 簡單構建 Node web服務器

.git bash 執行代碼生成:css

./makeJs.sh

 

生成文件以下:html

訪問:http://127.0.0.1:3030/index.html
node

makeJs.sh  代碼以下:git

#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  文件載入:(推薦)json

// --------
#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 接口批量測試bash

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

  使用 curl 測試post請求json接口
   app

./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
相關文章
相關標籤/搜索