花了點(diǎn)時(shí)間做了一個(gè)小東西,希望對(duì)你們理解node.js有幫助?。?!
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:主機(jī)域名、網(wǎng)絡(luò)空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、漢臺(tái)網(wǎng)站維護(hù)、網(wǎng)站推廣。
服務(wù)器代碼: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"); //監(jiān)聽器 request.addListener("data", function(postDataChunk) { console.log("獲取所有的數(shù)據(jù):"+postDataChunk); postData += postDataChunk; console.log('賬號(hào)是:'+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);
運(yùn)行方法代碼:requestHandlers.js
var querystring = require("querystring"); var fs = require("fs"); //獲取文件上傳模塊 var formidable = require("formidable"); //登陸頁(yè)面 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(); } //上傳頁(yè)面 function start(response, postData) { var body2 = ''+ ''+ ''+ ''+ ''+ '你好'+postData.split('&')[0].split('=')[1]+',歡迎登陸
運(yùn)行:
頁(yè)面展示:
登陸之后:
后臺(tái)數(shù)據(jù):
上傳成功頁(yè)面:
后臺(tái)數(shù)據(jù):
這些文件必須放在node.js的安裝目錄下
不要忘記在C盤創(chuàng)建文件夾:tmp。
下面是上傳的附件: