node.js初体验
最近想私下做些项目充实下自己空余时间,闲逛github发现node.js很火闲来没事参合一下。
##node.js简介 Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
##安装node.js
git clone https://github.com/joyent/node.git
//不一定是稳定版
稳定版如下方式:
wget http://nodejs.org/dist/v0.10.5/node-v0.10.5.tar.gz
tar -zxvf node-v0.10.5.tar.gz
cd node-v0.10.5
./configure
make
make install
##hello world vim hello.js:
console.log("Hello World");
通过node.js来执行
node hello.js
正常的话,就会在终端输出Hello World
##安装模块
npm install <package>
在下面的应用中会用到formidable
npm install formidable
##一个完整的基于Node.js的web应用
####index.js:
var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");
var handle = {};
handle["/"] = requestHandlenpm install <package.json folder>rs.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
handle["/show"] = requestHandlers.show;
server.start(router.route, handle);
####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;
####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;
####requestHandlers.js:
var querystring = require("querystring");
var fs = require("fs");
var formidable = require("formidable");
function start(response, request) {
console.log("Request handler 'start' was called");
var content = "empty";
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">'+
/*'<textarea name="text"></textarea>'+*/
'<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' />");
response.end();
});node-js-demo.tar.gz
}
function show(response, request) {
console.log("Request handler 'show' was called.");
fs.readFile("/tmp/test.png", "binary", function(error, data) {
if(error) {
console.log("readFile error");
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(error + "\n");
response.end();
} else {
response.writeHead(200, {
"Content-Type": "image/png",
"Content-Length": data.length,
});
response.write(data,"binary");
response.end();
}
});
}
exports.start = start;
exports.upload = upload;
exports.show = show;
参考资料
1.名词王国的死刑
资源打包下载