使用nodejs写的很简单的静态服务器,没有做cluster处理,没有做缓存处理,不支持访问文件夹,功能只有一个,就是获取到文件后再返回文件内容。

var fs = require('fs');
var url = require('url');
var http = require('http');
var path = require('path');
var mime = require("./mime").mime;
var HTTP_SERVER_PORT = 3000;
/*
默认页 index.html
*/ var appPath = process.argv[2];
appPath = appPath.replace(/\//g, '\\'); var proxy = http.createServer(function (req, res) {
var reqUrl = url.parse(req.url);
if (reqUrl.pathname == "/") {
reqUrl.pathname = "index.html";
}
var realPath = path.join(appPath, decodeURIComponent(reqUrl.pathname));
fs.exists(realPath, function (exists) {
if (exists && isFileExists(realPath)) {
fs.readFile(realPath, "binary", function (err, file) {
if (!err) {
var contentType = mime.lookupExtension(path.extname(realPath));
res.writeHead(200, {
"Content-Type": contentType + "; charset=utf-8",
"Content-Length": Buffer.byteLength(file, 'binary'),
"Server": "NodeJs(" + process.version + ")"
});
res.write(file, "binary");
res.end();
}
});
} else {
res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
res.write(JSON.stringify({ "error": "file not found: " + realPath }));
res.end();
}
});
}); proxy.on('clientError', function (exception, socket) {
console.log("an error has occurred in client ", exception);
}) proxy.listen(HTTP_SERVER_PORT, function () {
console.log('Http Server running on port:' + HTTP_SERVER_PORT);
}); function isFileExists(filePath) {
return fs.existsSync(filePath) && fs.statSync(filePath).isFile();
};

把代码给改了一下,但是处理流媒体的时候还是有问题。

var proxy = http.createServer(function (req, res) {
var reqUrl = url.parse(req.url);
if (reqUrl.pathname == "/") {
reqUrl.pathname = "index.html";
}
var realPath = path.join(appPath, decodeURIComponent(reqUrl.pathname));
fs.exists(realPath, function (exists) {
if (exists && isFileExists(realPath)) {
var contentType = mime.lookupExtension(path.extname(realPath));
res.writeHead(200, {
"Content-Type": contentType + "; charset=utf-8",
"Content-Length": fs.statSync(realPath).size,
"Server": "NodeJs(" + process.version + ")"
});
console.log(realPath);
var fileStream = fs.createReadStream(realPath);
fileStream.pipe(res, { end: false });
fileStream.on('end', function () {
res.end();
})
} else {
res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
res.write(JSON.stringify({ "error": "file not found: " + realPath }));
res.end();
}
});
});

nodejs-http-server-2015年1月14日-104102.rar

nodejs http静态服务器的更多相关文章

  1. 安装nodeJs静态服务器(NodeJs Express MVC 框架)

    安装 NodeJs Express MVC 框架   新建项目文件夹   打开cmd 执行以下操作:   一.使用Express框架 1)安装express3 $: npm install -g ex ...

  2. Nodejs实现web静态服务器对多媒体文件的支持

    前几天,一个同事说他写的web静态服务器不支持音视频的播放,现简单实现一下. 原理:实现http1.1协议的range部分. 其实这一点都不神秘,我们常用的下载工具,如迅雷,下载很快,还支持断点续传, ...

  3. nodejs创建http服务器

    之前有简单介绍nodejs的一篇文章(http://www.cnblogs.com/fangsmile/p/6226044.html) HTTP服务器 Node内建有一个模块,利用它可以很容易创建基本 ...

  4. 用http-server 创建node.js 静态服务器

    今天做一本书上的例子,结果代码不能正常运行,查询了一下,是语法过时了,书其实是新买的,出版不久. 过时代码如下 var connect=require('connect'); connect.crea ...

  5. http-server 基于nodejs的http服务器

    http-server所用场景: 作为前端的同学来说,想要运行一段代码,但又没有必要使用tomcat或是Apache http server,这个时候,一个简单的轻量的http-server就能搞定. ...

  6. node的两种随起随用静态服务器搭建

      一. anywhere Anywhere是一个随启随用的静态服务器,它可以随时随地将你的当前目录变成一个静态文件服务器的根目录. 1.确定电脑上安装了node.js 2.在当前所在项目文件夹下输入 ...

  7. NodeJS搭建HTTPS服务器

    [NodeJS搭建HTTPS服务器] http://cnodejs.org/topic/54745ac22804a0997d38b32d

  8. nodejs,node原生服务器搭建实例

    nodejs,node原生服务器搭建实例

  9. nginx 一二事(2) - 创建虚拟静态服务器

    一.什么是nginx 是一个C语言开发的HTTP反向代理服务器,性能非常高 一个俄罗斯的哥们开发的,官方提供的测试性能能够达到5W的并发,我的天呐~,实际测试差不多是2W,而淘宝的牛人可以优化到200 ...

随机推荐

  1. Meet Apache Wicket

    第一次接触Wicket,如此多的内容是文字,的原贴,希望大家指正 Meet Apache Wicket By JonathanLocke, original author of Wicket 乔纳森· ...

  2. VS解决BEX错误但不能关闭DEP保存

    报道近期计划BEX错误: 问题签名: 问题事件名称: BEX 应用程序名: Auth.exe 应用程序版本号: 0.0.0.0 应用程序时间戳: 546d9e0c 故障模块名称: Auth.exe 故 ...

  3. CENTOS安装部署zabbix

    果学网 -专注IT在线www.prismcollege.com 參考原文:http://www.showerlee.com/archives/13 RHCE过的EMAIL已经下来.所以近期闲了就准备把 ...

  4. oracle12c(oracle12.1.0.1.0)安装指南--实测OEL5.9(RH5)

    [root@oel ora12c]# uname -a Linux oel 2.6.39-300.26.1.el5uek #1 SMP Thu Jan 3 18:31:38 PST 2013 x86_ ...

  5. Red Gate系列之八 SQL Connect 1.1.1.19 Edition 数据库连接及操作工具 完全破解+使用教程

    原文:Red Gate系列之八 SQL Connect 1.1.1.19 Edition 数据库连接及操作工具 完全破解+使用教程 Red Gate系列之八 SQL Connect 1.1.1.19 ...

  6. Android实战技术:IPC方式简介教程

    非实时,通知性的方式 第一种方式就是Intent,Intent可以非常方便的通讯,但是它是非实时的,无法进行实时的像函数调用那样的实时的通讯. 实时的函数调用 但是IPC的根本目的还是为了实现函数的调 ...

  7. Android该系统提供的服务--Vibrator(振子)

    Android该系统提供的服务--Vibrator(振子) --转载请注明出处:coder-pig Vibrator简单介绍与相关方法: watermark/2/text/aHR0cDovL2Jsb2 ...

  8. 802.11(wi-fi)的PHY层(编码与调制方法)

    版本概要: 802.11-2007是目前的基础版本,之前的过时版本不考虑. 2009是较新的版本,就是目前最普及的802.11n.(100Mb/s) 2012就是传说中的802.11ac,工作在5G, ...

  9. [文学阅读] METEOR: An Automatic Metric for MT Evaluation with Improved Correlation with Human Judgments

    METEOR: An Automatic Metric for MT Evaluation with Improved Correlation with Human Judgments Satanje ...

  10. F5当刷新页面,出现“要再次显示此页,web该浏览器,你曾经有过发送消息再次提交...点击重试",如何防止此对话框解决方案的出现,

    如何取消刷新页面弹出"重试"对话? 找了好多类似的问题都没有我想要的答案,请大家看清楚再回答. 比方说,登录过程中,成功之后转向还有一个页面success.jsp, 这时,假设刷新 ...