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=
while [ "$#" -ge "" ];do
echo "参数序号为 $count 是 $1"
curl -i -X POST -H 'Content-type':'application/json' -d {"BTime":""$btime""} $ let count=count+
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(,{'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(".")+,surl.length);
respone.writeHead(,{'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(,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
Shell 简单构建 Node web服务器的更多相关文章
- 一个简单的Java web服务器实现
前言 一个简单的Java web服务器实现,比较简单,基于java.net.Socket和java.net.ServerSocket实现: 程序执行步骤 创建一个ServerSocket对象: 调用S ...
- JSP学习笔记(三):简单的Tomcat Web服务器
注意:每次对Tomcat配置文件进行修改后,必须重启Tomcat 在E盘的DATA文件夹中创建TomcatDemo文件夹,并将Tomcat安装路径下的webapps/ROOT中的WEB-INF文件夹复 ...
- Golang学习-第二篇 搭建一个简单的Go Web服务器
序言 由于本人一直从事Web服务器端的程序开发,所以在学习Golang也想从Web这里开始学起,如果对Golang还不太清楚怎么搭建环境的朋友们可以参考我的上一篇文章 Golang的简单介绍及Wind ...
- C语言构建小型Web服务器
#include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include <string ...
- Windows窗体应用程序(非Console)使用libuv实现简单的异步WEB服务器
libuv是一个很强大的异步处理框架(严格意义上不能叫框架,其实就是一组异步函数库,当然框架这东西有各种各样的定义和理解_^...),最初的的目的是用于NODEJS的异步处理,不过因为它是一个独立的项 ...
- NODE编程(三)--构建Node Web程序1
一.HTTP服务器的基本知识 1.Node如何向开发者呈现HTTP请求 Node中的http模块提供了HTTP服务器和客户端接口: var http = require('http'); 创建HTTP ...
- python简单搭建HTTP Web服务器
对于Python 2,简单搭建Web服务器,只需在i需要搭建Web服务器的目录(如C:/ 或 /home/klchang/)下,输入如下命令: python -m SimpleHTTPServer 8 ...
- 搭建一个简单的node.js服务器
第一步:安装node.js.可以去官网:https://nodejs.org/en/进行下载. 查看是否成功,只需在控制台输入 node -v.出现版本号的话,就证明成功了. 第二步:编写node.j ...
- node - web 服务器 、server 服务器
node 作为服务端 - 基本使用 1. web 服务器 web.html <!DOCTYPE html> <html> <head> <meta chars ...
随机推荐
- C# 委托链(多播委托)
委托既可以封装一个方法,又可以对同一类型的方法进行封装,它就是多播委托 using System; using System.Collections.Generic; using System.Lin ...
- docker (2) 通用/镜像命令
Docker 的常用命令: (1)Docker help 命令: 可以查看有关docker的所有操作命令: (2)docker COMMAND -–help 查看docker 的某项命令的帮助文档 ...
- .Net 委托 delegate 学习
一.什么是委托: 委托是寻址方法的.NET版本,使用委托可以将方法作为参数进行传递.委托是一种特殊类型的对象,其特殊之处在于委托中包含的只是一个活多个方法的地址,而不是数据. 二.使用委托: 关键 ...
- JavaScript局部变量变量和函数命名提升
之前接触了一些javascript局部变量命名提升的问题但是一直没有总结今天特地好好总结一下 变量提升 一个变量的作用域是程序源代码中定义的这个变量的区域.全局变量拥有全局作用域,在javascrip ...
- flex页面布局练习--知乎
采用flexbox弹性容器 在手机端进行页面布局 样本地址: http://tpl.zhuamimi.cn/%E6%89%8B%E6%9C%BA%E7%AB%AF%E9%A1%B5%E9%9D%A2- ...
- Dotspatial 要素重叠分析
private void toolStripButton30_Click(object sender, EventArgs e) { //面状重叠分析 if (mapMain.Layers.Count ...
- 啰嗦的 java,简洁的 lombok —— lombok 的使用及简单实现单例模式注解
lombok 是什么? lombok 是一个非常神奇的 java 类库,会利用注解自动生成 java Bean 中烦人的 Getter.Setting,还能自动生成 logger.ToString.H ...
- The xp_cmdshell proxy account information cannot be retrieved or is invalid. Verify that the '##xp_cmdshell_proxy_account##' credential exists and contains valid information.
In one of our recent migrations, we got the following error when the client tried to fire xp_cmdshel ...
- eclipse导入maven项目,但无法编译的问题
同事今天从git 导入项目到eclipse 后,发现项目所依赖的包找不到依赖,初步判定是maven的依赖没有导入项目中. 最终发现,在项目中的.classpath 文件加入以下代码即可解决问题. &l ...
- objective-c高级编程 笔记
引用计数:通过给对象计数标志,来判断是否释放对象 注:只能释放自己持有的对象 id obj = [NSMutableArray array] 如obj这个对象,并不是你所持有的对象,所以你无法进行释放 ...