1 任务内容: 使用任何语言或脚本,开发一个静态文件Web服务程序,要求性能尽可能好
2 任务的难度: 五星
3 任务的目的: 学习 Linux 下编程
4 任务所涉及的软件: 任何语言,推荐 node.js
5 任务将大致消耗的时间: 2-3天
[五星]开发一个静态文件Web服务程序,要求性能尽可能好
- oneleaf
- 论坛管理员
- 帖子: 10448
- 注册时间: 2005-03-27 0:06
- 系统: Ubuntu 12.04
- mswwjick
- 帖子: 645
- 注册时间: 2009-01-28 21:42
- qy117121
- 论坛版主
- 帖子: 50559
- 注册时间: 2007-12-14 13:40
- 系统: Winbuntu
- 来自: 志虚国乌由市
- 联系:
Re: [五星]开发一个静态文件Web服务程序,要求性能尽可能好
我码盲,我路过
- eexpress
- 帖子: 58428
- 注册时间: 2005-08-14 21:55
- 来自: 长沙
- 月下叹逍遥
- 论坛版主
- 帖子: 33994
- 注册时间: 2010-10-07 14:23
- 系统: Archdows10
- 来自: 某系某星某洲某国某省某市
- 联系:
- oneleaf
- 论坛管理员
- 帖子: 10448
- 注册时间: 2005-03-27 0:06
- 系统: Ubuntu 12.04
Re: [五星]开发一个静态文件Web服务程序,要求性能尽可能好
代码: 全选
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服务程序,要求性能尽可能好
支持神掐。
◎当我站在道德的高度上俯视别人的时候,发现自己是多么渺小。
♥执着但不偏激,反对而不排斥,坚决捍卫矛盾体的存在方式。
★★★天气预报★★★
fcitx-yatable一个可以使用的码表输入法
[教程]几个实例攻克软件编译难关
Gentoo Development Guide
字体相关
♥执着但不偏激,反对而不排斥,坚决捍卫矛盾体的存在方式。
★★★天气预报★★★
fcitx-yatable一个可以使用的码表输入法
[教程]几个实例攻克软件编译难关
Gentoo Development Guide
字体相关
- oneleaf
- 论坛管理员
- 帖子: 10448
- 注册时间: 2005-03-27 0:06
- 系统: Ubuntu 12.04
Re: [五星]开发一个静态文件Web服务程序,要求性能尽可能好
加了mime的自动文件类型判断:
#安装 node.js 的包管理器 npm ,并利用npm去安装mime模块
#sudo apt-get install nodejs
sudo apt-get install npm
sudo npm install 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服务程序,要求性能尽可能好
我只会用python写一个二行的。。。