node.js的文件上传

[size=xx-large]首先为大家推荐一些nodejs的有关资源.[/size]
[b]node.js中文社区: [url]http://cnodejs.org/[/url]
Node.js入门教程(E文): [url]http://nodebeginner.org/[/url]
中文用户手册: [url]http://cnodejs.org/cman/[/url]
node.js API文档: [url]http://cnodejs.org/api/[/url]
npm依赖管理工具(node 中的RubyGems): [url]http://npmjs.org/[/url]
Node Cloud(全面的node.js资料库): [url]http://www.nodecloud.org/[/url][/b]

[b][size=large]1.什么是node.js[/size][/b]
Node.js是一个服务器端框架,基于Google的V8 JavaScript引擎创建,旨在利用事件触发、非阻塞的I/ O帮助开发人员构建高度可伸缩的网络程序
[b][size=large]2.代码[/size][/b]

var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");
var handle = {}; //包含处理函数,此处不能声明为 var handle = [];原因:[]的index只接受数字。{}的index才支持任意可hash的类型。
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
handle["/show"] = requestHandlers.show;
server.start(router.route,handle);



var http = require("http");
var url = require("url");
var formidable = require("formidable"); //必须要下在formidable模块,可以使用npm下载

function start(route,handle){
function onRequest(req,res){
pathname = url.parse(req.url).pathname; //得到路径
var postData = "";
console.log("Pathname--------------:" + pathname + " is coming");
route(handle,req,res,pathname); //调用路由函数
}
http.createServer(onRequest).listen(8080);
console.log("server is starting");

};

exports.start = start; //暴露start函数



function route(handle,req,res,pathname){
console.log("route a request for: " + pathname)
if(typeof handle[pathname] === 'function'){
handle[pathname](req,res);
return;
}else{
console.log("No Request Path----???????");
res.writeHead(200,{"Content-Type":"text/html"});
res.write("404 Not Found");
res.end();
}
}
exports.route = route;



var querystring = require("querystring"),
fs = require("fs"),
exec = require("child_process").exec,
formidable = require("formidable");
function start(req,res){
console.log("Request handler 'start' was called.");

var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" content="text/html; '+
'charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload" enctype="multipart/form-data" method="post">'+
'<input type="file" name="upload" multiple="multiple"/>'+
'<input type="submit" value="UploadPic" />'+
'</form>'+
'</body>'+
'</html>';

res.writeHead(200, {'Content-Type' : 'text/html'})
res.write(body);
res.end();
}
function upload(req,res){
console.log("Upload:" + req + " ---- ===");
var form = new formidable.IncomingForm();
form.parse(req,function(error,fields,files){
console.log("parsing is over");
fs.renameSync(files.upload.path,"/tmp/test.png");
res.writeHead(200,{"Content-Type":"text/html"});
res.write("received image:<br/>");
res.write("<img src='/show'/>");
res.end();
});;
}
function show(req,res){
console.log("show");
fs.readFile("/tmp/test.png","binary",function(error,file){
if(error){
res.writeHead(500,{"Content-Type":"text/plain"});
res.write(error+"\n");
res.end();
}else{
res.writeHead(200,{"Content-Type":"image/jpg"});
res.write(file,"binary");
res.end();
}
});
}

exports.start = start;
exports.upload = upload;
exports.show = show;




Administrator@WIN-23C1Q4GKQ4G ~
$ node /example/index.js
server is starting
Pathname--------------:/start is coming
route a request for: /start
Request handler 'start' was called.
Pathname--------------:/upload is coming
route a request for: /upload
Upload:[object Object] ---- ===
parsing is over
Pathname--------------:/show is coming
route a request for: /show
show

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值