Shell 简单构建 Node web服务器
.git bash 执行代码生成:
1 | . /makeJs .sh |
生成文件如下:
访问:http://127.0.0.1:3030/index.html
makeJs.sh 代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #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 接口批量测试
1 | . /makePost .sh http: //192 .168.3.52 /ad/customer/res |
使用 curl 测试post请求json接口
1 | . /makePost .sh |
1 2 3 4 5 6 7 | 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 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南