nodejs的简单服务器程序
下面是参考<Jquery、Jquery UI 及Jquery Mobile>一书中的nodej服务器程序
var http = require('http'),
url = require('url'),
fs = require('fs');
http.createServer(function (req, res) {
var reqData = {
url: url.parse(req.url, true),
method: req.method,
headers: req.headers },
path = reqData.url.pathname;
if(path.match(/^\/[0-9a-z\-]+\.(html)|(json)|(xml)$/))
fs.readFile('.' + path, function (err, data) {
if (err) {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('not found');
}
else {
if(path.split('.')[1] == 'html')
res.writeHead(200, {'Content-Type': 'text/html'});
else if(path.split('.')[1] == 'xml')
res.writeHead(200, {'Content-Type': 'application/xml'});
else
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(data);
}
});
else if(path == '/return-http-headers') {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(reqData));
}
else if(path == '/sleep') {
var endTime = new Date().getTime() + 2000;
while (new Date().getTime() < endTime);
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('slow response');
}
else if(path == '/validate') {
var keys = [];
for(var key in reqData.url.query) {
if(reqData.url.query[key] == '')
keys.push(key);
}
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(keys));
}
else if(path == '/redirect') {
res.writeHead(302, {
'Location': '/test-values.json' });
res.end();
}
else if(path == '/fail\-on\-purpose') {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('unexpected" error');
}
else {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('not found');
}
}).listen(1337, "localhost");
console.log('Server running at http://localhost:1337/');
nodejs的简单服务器程序的更多相关文章
- Socket编程回顾,一个最简单服务器程序
第一次接触服务器是快毕业的时候,是不是有点晚(# ̄ω ̄),这也导致工作方向一直没考虑网络编程这块,做了好多其他没啥“意思”的技术. 之前看到一篇博文提到程序猿80%都是庸才,10%是人才,10%是天才 ...
- nodeJs搭建简单服务器
实现简单的例子:hello word 把第一个文件命名为server.js 在server.js里写入 var http = require("http"); http.crea ...
- 最简单的回射客户/服务器程序、time_wait 状态
下面通过最简单的客户端/服务器程序的实例来学习socket API. echoser.c 程序的功能是从客户端读取字符然后直接回射回去. C++ Code 1 2 3 4 5 6 7 8 9 10 ...
- Linux下select的用法--实现一个简单的回射服务器程序
1.先看man手册 SYNOPSIS /* According to POSIX.1-2001 */ #include <sys/select.h> / ...
- nodejs学习之简单服务器的编写
不废话了!直接上代码: var http = require('http'); //导入模块http http.createServer(function(request,response){ res ...
- Socket创建简单服务器和客户端程序
使用Socket编程创建简单服务器和客户端 要知道的 Socket-AddressFamily, SocketType, ProtocolType https://blog.csdn.net/weix ...
- Node安装及搭建简单服务器
注:本文安装系统为mac,windows及其他系统下载对应安装包 ,mac下载后的安装包为apk文件,windows为msi文件. 安装 1.在网上下载node安装包,官方网站2.双击下载文件,按步骤 ...
- nodejs搭建web服务器初级
nodejs搭建简单的web服务器 1.1简介 Node.js是基于Chrome JavaScript运行时建立的一个平台,实际上它是对Google Chrome V8引擎进行了封装,它主要用于创建快 ...
- Nodejs创建HTTPS服务器
Nodejs创建HTTPS服务器 从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Jav ...
随机推荐
- 在mac中导入hadoop2.6.0源代码至eclipse
一.环境准备 1.安装jdk.maven等 2.下载hadoop源代码,并解压 3.将tools.jar复制到Classes中,具体原因见http://wiki.apache.org/hadoop/H ...
- 【Nutch2.2.1基础教程之1】nutch相关异常
1.在任务一开始运行,注入Url时即出现以下错误. InjectorJob: Injecting urlDir: urls InjectorJob: Using class org.apache.go ...
- 浅析 JavaScript 组件编写
之前因项目需要也编写过一些简单的JS组件,大多是基于JQuery库的,一直也没有过总结,导致再次写到的时候还去Google, 近日看到一个文章总结的挺好,拿过整理一下做个备忘. 此次同样是基于jque ...
- 图形用户界面入门:EasyGui - 零基础入门学习Python035
图形用户界面入门:EasyGui 让编程改变世界 Change the world by program 今天我们来谈谈图形用户界面编程,也就是我们常说的GUI(Graphical User Inte ...
- 无法读取配置节 system.serviceModel 因为它缺少节声明的解决方法
无法读取配置节 system.serviceModel 因为它缺少节声明的解决方法,需要的朋友可以参考下 在Windows Server2008 R2中的IIS7中部署WCF服务时报出如题错误: HT ...
- HoG feature for human detection(HoG 行人识别)
本文大部分内容总结于其他文章 1.介绍 HOG(Histogram of Oriented Gradient)是2005年CVPR会议上,法国国家计算机科学及自动控制研究所的Dalal等人提出的一种解 ...
- 在Fedora 20下编译安装QEMU
由于OpenStack的流行,现在越来越多的人关注QEMU,最近发行了新版本1.7,下面就说Fedora下的安装过程: 首先需要安装基本的编译软件和git sudo yum install gcc m ...
- 在Android模拟器中经常出现以下错误 timeout Launch canceled!
Failed to install MainActivity.apk on device 'emulator-5554': timeoutLaunch canceled! 解决方法: window-& ...
- Android使用xml中定义的动画效果
Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.zqrl_out); animation.setFil ...
- PowerShell_零基础自学课程_5_自定义PowerShell环境及Powershell中的基本概念
PowerShell_零基础自学课程_5_自定义PowerShell环境及Powershell中的基本概念 据我个人所知,windows下的cmd shell除了能够通过修改系统参数来对其中的环境变量 ...