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 ...
随机推荐
- 使用visualStudio2017创建一个简单的控制台程序
步骤: 1. 打开visual studio开发工具 2. 选择文件>新建>项目 如下图所示: 3. 选择window金典桌面>控制台应用程序 并填写好想项目名称和选择项目存储地址 ...
- 【MySQL】MySQL的执行计划及索引优化
我们知道一般图书馆都会建书目索引,可以提高数据检索的效率,降低数据库的IO成本.MySQL在300万条记录左右性能开始逐渐下降,虽然官方文档说500~800w记录,所以大数据量建立索引是非常有必要的. ...
- 折腾Java设计模式之状态模式
原文地址 折腾Java设计模式之状态模式 状态模式 在状态模式(State Pattern)中,类的行为是基于它的状态改变的.这种类型的设计模式属于行为型模式.在状态模式中,我们创建表示各种状态的对象 ...
- 解决vue数据渲染过程中的闪动问题
关键代码 主要解决vue双大括号{{}}在数据渲染和加载过程中的闪动问题,而影响客服体验. html代码: <span class="tableTitle selftab" ...
- CSS3 弹性盒子
理解: CSS3 弹性盒( Flexible Box 或 flexbox),是一种当页面需要适应不同的屏幕大小以及设备类型时确保元素拥有恰当的行为的布局方式. 设置弹性盒子: 弹性盒子由弹性容器(Fl ...
- 报错TypeError: $(...).live is not a function解决方法
报错的原因是这个方法在jquery1.7以后就被废除了, 1.7以后的版本改用.on()方法 之前的用法: .live(events, function) 新方法: .on(eventType, se ...
- 在chrome 怎么通过ajax请求加载本地文件
在chrome下面用Jquery 的load方法加载本地的html文件时会报错 我百度了一下是因为 谷歌浏览器内核为了安全机制,不允许这样方式访问其他页面,但是可以通过加 --enable-file- ...
- 一个特殊的SQL Server阻塞案例分析
上周,在SQL Server数据库下面遇到了一个有意思的SQL阻塞(SQL Blocking)案例.其实个人对SQL Server的阻塞还是颇有研究的.写过好几篇相关文章. 至于这里为什么要总结一下这 ...
- Powershell-获取MAC地址对应IP信息
因业务需要在DHCP服务器上绑定设备MAC,提示"指定的IP地址或硬件地址正被其他客户端使用",与业务同事沟通之前该设备做过地址保留,具体对应地址遗忘了. 问题描述: a.按照用户 ...
- fastjson SerializerFeature详解