[五星]开发一个静态文件Web服务程序,要求性能尽可能好

除了美化之外,还可以来尝试挑战一下任务
回复
头像
oneleaf
论坛管理员
帖子: 10441
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

[五星]开发一个静态文件Web服务程序,要求性能尽可能好

#1

帖子 oneleaf » 2012-06-29 9:35

1 任务内容: 使用任何语言或脚本,开发一个静态文件Web服务程序,要求性能尽可能好

2 任务的难度: 五星

3 任务的目的: 学习 Linux 下编程

4 任务所涉及的软件: 任何语言,推荐 node.js

5 任务将大致消耗的时间: 2-3天
头像
mswwjick
帖子: 645
注册时间: 2009-01-28 21:42

Re: [五星]开发一个静态文件Web服务程序,要求性能尽可能好

#2

帖子 mswwjick » 2012-06-29 11:56

不是很懂, :em06 :em06
头像
qy117121
论坛版主
帖子: 50528
注册时间: 2007-12-14 13:40
系统: Winbuntu
来自: 志虚国乌由市
联系:

Re: [五星]开发一个静态文件Web服务程序,要求性能尽可能好

#3

帖子 qy117121 » 2012-06-29 11:57

我码盲,我路过 :em06
渠月 · QY   
本人只会灌水,不负责回答问题
无聊可以点一下→ http://u.nu/ubuntu

Ubuntu 20.04 快速设置指南,请配合浏浏览器自动翻译使用

邮箱 chuan@ubuntu.org.cn
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: [五星]开发一个静态文件Web服务程序,要求性能尽可能好

#4

帖子 eexpress » 2012-06-29 13:56

看来 oneleaf 又玩起 node.js了
非it围观。
● 鸣学
头像
月下叹逍遥
论坛版主
帖子: 33994
注册时间: 2010-10-07 14:23
系统: Archdows10
来自: 某系某星某洲某国某省某市
联系:

Re: [五星]开发一个静态文件Web服务程序,要求性能尽可能好

#5

帖子 月下叹逍遥 » 2012-06-29 13:59

围观 :em04
浮生七十今三十,从此凄惶未可知
头像
oneleaf
论坛管理员
帖子: 10441
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

Re: [五星]开发一个静态文件Web服务程序,要求性能尽可能好

#6

帖子 oneleaf » 2012-06-29 15:49

代码: 全选

var url = require("url");
var http = require('http');
var path = require("path");
var fs = require("fs");
var root = '.';

function onRequest (request, response) {
    var pathname = url.parse(request.url).pathname;
    var realPath = path.join(root,path.normalize(pathname));
    path.exists(realPath, function (exists) {
        if (!exists) {
            response.writeHead(404, {'Content-Type': 'text/plain'});
            response.end("404 Not found");
        } else {
            fs.stat(realPath,function(err, stats){
                if (stats.isDirectory()) {
                   fs.readdir(realPath, function (err, files){
                       response.writeHead(200, {'Content-Type': 'text/html'});
                       response.write('<html><head></head><body>');
                       response.write('<p><a href="..">..</a></p>');
                       files.forEach(function(file) {
                           response.write('<p><a href="'+path.join(realPath,file)+'">'+file+'</a></p>');
                       });
                       response.write('</body></html>');              
                       response.end();
                    });
                } else {   
                    fs.readFile(realPath, "binary", function (err, file) {
                       response.writeHead(200, {'Content-Type': 'application/octet-stream'});
                       response.write(file, "binary");
                       response.end();
                    });
                }
            });
        }
    });
};

http.createServer(onRequest).listen(8888);
写了一个超简单的。
先安装node.js环境。 sudo apt-get install nodejs
完毕后将上面代码保存为 index.js ,运行:node index.js 然后在浏览器访问 http://127.0.0.1:8888
其中代码里面的root为静态文件的根目录,可以自行修改。
头像
YeLee
论坛版主
帖子: 26406
注册时间: 2008-08-13 8:48
系统: Fundu i64
来自: 东海硇州,一双管钥。
联系:

Re: [五星]开发一个静态文件Web服务程序,要求性能尽可能好

#7

帖子 YeLee » 2012-06-29 16:00

支持神掐。 :em04
◎当我站在道德的高度上俯视别人的时候,发现自己是多么渺小。
♥执着但不偏激,反对而不排斥,坚决捍卫矛盾体的存在方式。
★★★天气预报★★★
fcitx-yatable一个可以使用的码表输入法
[教程]几个实例攻克软件编译难关
Gentoo Development Guide
字体相关
头像
oneleaf
论坛管理员
帖子: 10441
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

Re: [五星]开发一个静态文件Web服务程序,要求性能尽可能好

#8

帖子 oneleaf » 2012-06-29 16:22

加了mime的自动文件类型判断:

#安装 node.js 的包管理器 npm ,并利用npm去安装mime模块
#sudo apt-get install nodejs
sudo apt-get install npm
sudo npm install mime

修正后的代码如下:

代码: 全选

var url = require("url");
var http = require('http');
var path = require("path");
var fs = require("fs");
var mime = require('mime');
var root = '.';

function onRequest (request, response) {
    var pathname = url.parse(request.url).pathname;
    var realPath = path.join(root,path.normalize(pathname));
    path.exists(realPath, function (exists) {
        if (!exists) {
            response.writeHead(404, {'Content-Type': 'text/plain'});
            response.end("404 Not found");
        } else {
            fs.stat(realPath,function(err, stats){
                if (stats.isDirectory()) {
                   fs.readdir(realPath, function (err, files){
                       response.writeHead(200, {'Content-Type': 'text/html'});
                       response.write('<html><head></head><body>');
                       response.write('<p><a href="..">..</a></p>');
                       files.forEach(function(file) {
                           response.write('<p><a href="'+path.join(realPath,file)+'">'+file+'</a></p>');
                       });
                       response.write('</body></html>');              
                       response.end();
                    });
                } else {   
                    fs.readFile(realPath, "binary", function (err, file) {
                       type=mime.lookup(realPath);
                       response.writeHead(200, {'Content-Type': type});
                       response.write(file, "binary");
                       response.end();
                    });
                }
            });
        }
    });
};

http.createServer(onRequest).listen(8888);
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

Re: [五星]开发一个静态文件Web服务程序,要求性能尽可能好

#9

帖子 枫叶饭团 » 2012-06-29 20:28

我只会用python写一个二行的。。。
回复