(六)node.js做的登录和上传图片的小作品
花了点时间做了一个小东西,希望对你们理解node.js有帮助!!!
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:主机域名、网络空间、营销软件、网站建设、汉台网站维护、网站推广。
服务器代码:server.js
var http = require("http"); var url = require("url"); function start(route, handle) { function onRequest(request, response) { var postData = ""; var pathname = url.parse(request.url).pathname; console.log("Request for " + pathname + " received."); if(pathname=='/start'){ //登录方法使用 request.setEncoding("utf8"); //监听器 request.addListener("data", function(postDataChunk) { console.log("获取所有的数据:"+postDataChunk); postData += postDataChunk; console.log('账号是:'+postData.split('&')[0].split('=')[1]+' 密码是: '+postData.split('&')[1].split('=')[1]); }); request.addListener("end", function() { route(handle, pathname, response, postData); }); }else{ //非登录方法使用 route(handle, pathname, response, request); } } http.createServer(onRequest).listen(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/html"}); response.write("404 Not found"); response.end(); } } exports.route = route;
访问的路径代码:index.js
var server = require("./server"); var router = require("./router"); var requestHandlers = require("./requestHandlers"); var handle = {} handle["/"] = requestHandlers.login; handle["/login"] = requestHandlers.login; handle["/start"] = requestHandlers.start; handle["/upload"] = requestHandlers.upload; handle["/show"] = requestHandlers.show; server.start(router.route, handle);
运行方法代码:requestHandlers.js
var querystring = require("querystring"); var fs = require("fs"); //获取文件上传模块 var formidable = require("formidable"); //登陆页面 function login(response,postData) { console.log("Request handler 'login' was called."); var body = ''+ ''+ ''+ ''+ ''+ ''+ ''+ ''; response.writeHead(200, {"Content-Type": "text/html"}); response.write(body); response.end(); } //上传页面 function start(response, postData) { var body2 = ''+ ''+ ''+ ''+ ''+ '你好'+postData.split('&')[0].split('=')[1]+',欢迎登陆
账号是: '+postData.split('&')[0].split('=')[1]+' 密码是: '+postData.split('&')[1].split('=')[1]+'
'+ ''+ ''+ ''; console.log("Request handler 'upload' was called."); response.writeHead(200, {"Content-Type": "text/html"}); response.write(body2); response.end(); } //上传方法 function upload(response, request) { console.log("Request handler 'upload' was called."); var form = new formidable.IncomingForm(); console.log("about to parse"); console.log("图片详细信息:"+form); form.parse(request, function(error, fields, files) { console.log("parsing done"); //修改图片名称 fs.renameSync(files.upload.path, "/tmp/test.jpg"); response.writeHead(200, {"Content-Type": "text/html"}); //显示图片名称 response.write("received p_w_picpath:
"); //显示图片 response.write(""); response.end(); }); } //展示方法 function show(response) { console.log("Request handler 'show' was called."); //寻找文件 fs.readFile("/tmp/test.jpg", "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": "p_w_picpath/jpg"}); response.write(file, "binary"); response.end(); } }); } exports.login = login; exports.start = start; exports.upload = upload; exports.show = show;
运行:
页面展示:
登陆之后:
后台数据:
上传成功页面:
后台数据:
这些文件必须放在node.js的安装目录下
不要忘记在C盘创建文件夹:tmp。
下面是上传的附件:
分享文章:(六)node.js做的登录和上传图片的小作品
URL地址:http://cdiso.cn/article/gcooji.html