本案例来自Node.js入门书籍:
http://www.nodebeginner.org/index-zh-cn.html
示例中只能上传并展示png图片,当然其他文件都是可行的,自己微调一下即可。
安装node.js:
# curl -sL https://rpm.nodesource.com/setup | bash - # yum install -y nodejs
防火墙打开8888端口;
Node.js自身处理上传文件会非常繁琐,可使用第三方的node-formidable来轻松处理:
npm install formidable
以下是代码:
Server端:
#server.js var http = require("http"); var url = require("url"); var order = 1; //请求次数 function start(route, handle) { function onRequest(request, response) { var postData = ""; var pathname = url.parse(request.url).pathname; //请求位置 console.log("Request for " + pathname + " received."); route(handle, pathname, response, request); //路由此次请求 console.log("Reauest received ~ +" + order); //请求次数 order++; } http.createServer(onRequest).listen(8888); //监听8888端口 console.log("Server has started"); } exports.start = start;
路由:
#router.js function route(handle, pathname, response, request) { console.log("About to route a request for " + pathname); if (typeof(handle[pathname]) === ‘function‘) { handle[pathname](response, request); //执行请求处理方法 } else { console.log("No request handler found for " + pathname); response.writeHead(404, { "Content-Type": "text/plain" }); response.write("404 Not found"); response.end(); } } exports.route = route;
Handler:
#requestHandlers.js var querystring = require("querystring"); var formidable = require("formidable"); var fs = require("fs"); function start(response) { 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">‘ + ‘<input type="submit" value="Upload file" />‘ + ‘</form>‘ + ‘</body>‘ + ‘</html>‘; response.writeHead(200, { "Content-Type": "text/html" }); response.write(body); response.end(); } function upload(response, request) { console.log("Request handler ‘upload‘ was called."); var form = new formidable.IncomingForm(); console.log("about to parse"); form.parse(request, function(error, fields, files) { console.log("parsing done"); fs.renameSync(files.upload.path, "/tmp/test.png"); //更名 response.writeHead(200, { "Content-Type": "text/html" }); response.write("received image:<br/>"); response.write("<img src=‘/show‘ />"); //调用show方法展示图片 response.end(); }); } function show(response) { var imagePath = "/tmp/test.png"; console.log("Request handler ‘show‘ was called."); fs.readFile(imagePath, "binary", function(error, file) { if (error) { response.writeHead(500, { "Content-Type": "text/plain" }); response.write(error + "\n"); response.end(); } else { response.writeHead(200, { "Content-Type": "image/png" }); response.write(file, "binary"); response.end(); } }); } exports.start = start; exports.upload = upload; exports.show = show;
入口文件:
#index.js var server = require("./server"); //服务端 var router = require("./router"); //路由 var requestHandlers = require("./requestHandlers"); //请求处理器 var handle = {}; handle["/"] = handle["/start"] = requestHandlers.start; handle["/upload"] = requestHandlers.upload; handle["/show"] = requestHandlers.show; server.start(router.route, handle); //初始函数 - 参数为路由方法及相应的处理方法
将这些扔到一个文件夹中,启动:
# node index.js
浏览器访问:http://yourhost:8888 或在其后加上/start 即可进入上传页面。
End.
时间: 2024-11-08 22:04:16