初涉Node.js
Node.js的是建立在Chrome的JavaScript的运行时,可方便地构建快速,可扩展的网络应用程序的平台。
在主要脚本(index.js)里引用http服务器模块并启动。
Node.js使用事件驱动、非阻塞I/ O模型,是轻量级、高效、完美的跨分布式设备运行数据密集型实时应用。
1、安装Node.js:http://www.nodejs.org/
测试安装正确性:安装之后会有一个命令行工具。打开命令行工具,然后输入node helloworld.js。
helloworld.js中只需简单的一句话:console.log("Hello World!");
2、构建应用的模块——一个基础的HTTP服务器(server.js)
var http = require("http");
function start() {
http.createServer(function (request, response) {
response.writeHead(200, { "Content-Type": "text/plain" });
response.write("Hello World!");
response.end();
}).listen(8888);
}
exports.start = start;
function start() {
http.createServer(function (request, response) {
response.writeHead(200, { "Content-Type": "text/plain" });
response.write("Hello World!");
response.end();
}).listen(8888);
}
exports.start = start;
var
server = require("./server");
server.start();
server.start();
测试:在命令行输入“node
index.js”,然后打开浏览器访问:http://localhost:8888/ ,网页显示“Hello
World!”。
这样我们就可以定义不同的模块分别放在不同的文件里了。
3、构建路由选择模块(router.js)
4、加入请求处理程序模块(requestHandlers.js)
下面以上传文件为例,展现各个模块间的关系。
server.js
var http
= require("http");
var url = require("url");
function start(route, handle) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
route(handle, pathname, response, request);
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
var url = require("url");
function start(route, handle) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
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/plain" });
response.write("404 Not found");
response.end();
}
}
exports.route = route;
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;
requestHandler.js
var querystring = require("querystring");
var fs = require("fs");
var formidable = require("formidable");
var sys = require("sys");
function start(response, postData) {
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="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();
form.uploadDir = "temp";
console.log("about to parse");
form.parse(request, function (error, fields, files) {
console.log("parsing done");
//sys.puts(sys.inspect(files.upload, true, null));
fs.renameSync(files.upload.path, "./temp/test.jpg");
response.writeHead(200, { "Content-Type": "text/html" });
response.write("received image: < br/>");
response.write(" < img src='/show' />");
response.end();
});
}
function show(response) {
console.log("Request handler 'show' was called.");
fs.readFile("./temp/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": "image/jpeg" });
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[/"] = requestHandlers.start;
handle[/start"] = requestHandlers.start;
handle[/upload"] = requestHandlers.upload;
handle[/show"] = requestHandlers.show;
server.start(router.route, handle);