nodejs http静态服务器
使用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静态服务器的更多相关文章
- 安装nodeJs静态服务器(NodeJs Express MVC 框架)
安装 NodeJs Express MVC 框架 新建项目文件夹 打开cmd 执行以下操作: 一.使用Express框架 1)安装express3 $: npm install -g ex ...
- Nodejs实现web静态服务器对多媒体文件的支持
前几天,一个同事说他写的web静态服务器不支持音视频的播放,现简单实现一下. 原理:实现http1.1协议的range部分. 其实这一点都不神秘,我们常用的下载工具,如迅雷,下载很快,还支持断点续传, ...
- nodejs创建http服务器
之前有简单介绍nodejs的一篇文章(http://www.cnblogs.com/fangsmile/p/6226044.html) HTTP服务器 Node内建有一个模块,利用它可以很容易创建基本 ...
- 用http-server 创建node.js 静态服务器
今天做一本书上的例子,结果代码不能正常运行,查询了一下,是语法过时了,书其实是新买的,出版不久. 过时代码如下 var connect=require('connect'); connect.crea ...
- http-server 基于nodejs的http服务器
http-server所用场景: 作为前端的同学来说,想要运行一段代码,但又没有必要使用tomcat或是Apache http server,这个时候,一个简单的轻量的http-server就能搞定. ...
- node的两种随起随用静态服务器搭建
一. anywhere Anywhere是一个随启随用的静态服务器,它可以随时随地将你的当前目录变成一个静态文件服务器的根目录. 1.确定电脑上安装了node.js 2.在当前所在项目文件夹下输入 ...
- NodeJS搭建HTTPS服务器
[NodeJS搭建HTTPS服务器] http://cnodejs.org/topic/54745ac22804a0997d38b32d
- nodejs,node原生服务器搭建实例
nodejs,node原生服务器搭建实例
- nginx 一二事(2) - 创建虚拟静态服务器
一.什么是nginx 是一个C语言开发的HTTP反向代理服务器,性能非常高 一个俄罗斯的哥们开发的,官方提供的测试性能能够达到5W的并发,我的天呐~,实际测试差不多是2W,而淘宝的牛人可以优化到200 ...
随机推荐
- 《JavaScript设计模式与开发实践》读书笔记之策略模式
1.策略模式 定义一系列算法,把它们一个个封装起来,并且使它们可以相互替换 1.1 传统实现 根据工资基数和年底绩效来发送年终奖 var calculateBonus= function (perfo ...
- 学习OpenCV第0天
自2011年接触OpenCV已经有几年了,一直停留在写一些小程序,利用手冊完毕一些任务,一直没有深入研究当中代码,现在毕业,但各种原因未能进入图像处理行业,故现重学OpenCV,包含分析代码,学习算法 ...
- in与exist , not in与not exist 的区别(转)
in和exists in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询.一直以来认为exists比in效率高的说法是不准确的. 如果查询的 ...
- jquery动态加入删除一行数据
<html> <head> <title>加入.删除一行</title> <meta http-equiv="content-type& ...
- 行人检测(Pedestrian Detection)资源整合
一.纸 评论文章分类: [1] D. Geronimo, and A. M.Lopez. Vision-based Pedestrian Protection Systems for Intellig ...
- 设计模式(一)工厂模式Factory(创建类型)
设计模式一 工厂模式Factory 在面向对象编程中, 最通常的方法是一个new操作符产生一个对象实例,new操作符就是用来构造对象实例的.可是在一些情况下, new操作符直接生成对象会带来一些问题. ...
- ORA-00913错误:PL/SQL: ORA-00913: too many values
ORA-00913错误 描写叙述:PL/SQL: ORA-00913: too many values 目标:编写一个能够循环插入数据的脚本 操作过程: SQL> desc tcustmer N ...
- MIPS台OpenWrt在系统内的路由器Rust应用程序开发
笔者:Liigo(庄小莉) 迄今:2014年9一个月17日本 (9一个月29日更新,11一个月19日本再次更新.在最后可用更新) 原文链接:http://blog.csdn.net/liigo/art ...
- hdu1711 Number Sequence
Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...
- 好玩的WPF第二弹:电子表字体显示时间+多彩呼吸灯特效button
我们先来看看Quartz MS字体动态显示系统时间的效果,难度相较于上一篇也要简单很多. 首先是定义一个TextBlock例如以下. <Grid> <TextBlock Name=& ...