您的位置:首页 > Web前端 > Node.js

Node 入门 示例程序

2014-08-17 10:00 288 查看
说明:本示例代码是按照Node入门教程编写的代码。是关于图片上传并在另一页面显示。

index.js内部代码:

//主文件 其他模块被主文件调用
var startServer = require('./server'); //调用启动HTTP服务器的模块
var route = require('./route'); //调用路由模块
var requestHandles = require('./requestHandles'); //路由后处理程序

var handle = {};
handle['/'] = requestHandles.start;
handle['/start'] = requestHandles.start;
handle['/upload'] = requestHandles.upload;
handle['/show'] = requestHandles.show;

startServer.startServer(route.route, handle);

server.js内部代码:
//HTTP服务器模块
var http = require('http');
var url = require('url');

function startServer(route, handle){ //把启动服务器程序放在函数中 参数为route模块的route函数
http.createServer(function(req, res){
var pathname = url.parse(req.url).pathname; //获得用户请求的url
console.log ("Request for "+pathname+"received");

route(handle, pathname, res, req); //调用路由模块中的route函数

}).listen(8888);
console.log("Server has be started");
}

exports.startServer = startServer; //导出这个模块 在主模块中调用

route.js内部代码:
/**
* 路由模块
*/
function route(handle, pathname, res, req){
console.log("About to route a request for:"+pathname);
if(typeof handle[pathname] === "function"){ //查看对应路径的属性值是否是定义的路由后函数
return handle[pathname](res, req); // 执行对应请求路径的函数
}else{
console.log("No request for handles found");
res.writeHead(404, {'Content-Type':'text/plain'});
res.write("404 Not found");
res.end();

}
}
exports.route = route;

requestHandles.js内部代码:
/**
* 请求处理程序
* 这样我们就可以把请求处理程序和路由模块连接起来,让路由“有路可寻”。
*/
//var exec = require('child_process').exec;
var querystring = require('querystring');
var fs = require('fs');
var formidable = require("formidable");

function start(res){
console.log("Request handles [start] has 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>';
/* exec('dir', function(error, stdout, stdin){
res.writeHead(200, {'Content-Type':'text/plain'});
res.write(stdout);
res.end();
});*/
res.writeHead(200, {'Content-Type':'text/html'});
res.write(body);
res.end();

}
function upload(res, req){
console.log("Request handles [upload] has called");
var form = new formidable.IncomingForm();
console.log("------"+form.uploadDir+"----------");
//临时文件写成功了,但是就是无法移动...
// 猜测是由于磁盘分区导致的,你的例子中也符合这个条件 C: -> E:
form.uploadDir = 'temp'; //改变formidable的默认零时文件夹路径,保证和目标目录处于同一个磁盘分区
console.log("------"+form.uploadDir+"----------");
console.log("about to parse");

form.parse(req, function(error, fields, files){
fs.renameSync(files.upload.path, "./temp/test2.jpg");
res.writeHead(200, {"Content-Type": "text/html"});
res.write("received image:<br/>");
res.write("<img src='/show' />");
res.end();
});
//res.writeHead(200, {'Content-Type':'text/plain'});
//res.write("you have set:" + querystring.parse(postData).text);
//res.end();
}

function show(res, postData) {
console.log("Request handler 'show' was called.");
fs.readFile("./temp/test2.jpg", "binary", function(error, file) {
if(error) {
res.writeHead(500, {"Content-Type": "text/plain"});
res.write(error + "\n");
res.end();
} else {
res.writeHead(200, {"Content-Type": "image/jpg"});
res.write(file, "binary");
res.end();
}
});
}

exports.start = start;
exports.upload = upload;
exports.show = show;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息