初学nodejs,看了一篇相关的文章觉得不错,在这里总结一下,也可以给和我一样的初学者提供一些参考。
以下为个人通过阅读相关文章,做的一个关于图片上传的小程序,虽然没涉及到数据库的操作,但基本架构已经五脏俱全。好了,废话不多说,上代码。
// 程序的入口文件: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);
// 服务器模块:server.js
var http = require("http");
var url = require("url");
function start(route, handle) {
http.createServer(function(request, response){
var pathname = url.parse(request.url).pathname;
if(pathname !== "/favicon.ico") {
console.log("Request for " + pathname + " recevied");
route(handle, pathname, response, request);
}
}).listen(8080);
console.log("Server has started and listen 8080 port");
}
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;
// 请求处理文件:requestHandlers.js
var formidable = require("formidable");
var querystring = require("querystring");
var fs = require("fs");
var util = require("util");
function start(response, request) {
console.log("Request handler 'start' was called.");
var body = '' +
'' +
'
' +
'' +
'' +
'
' +
'' +
'';
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, "D:\\tmp/test.jpg");
var readStream = fs.createReadStream(files.upload.path);
var writeStream = fs.createWriteStream("D:\\tmp/test.jpg");
util.pump(readStream, writeStream, function() {
fs.unlinkSync(files.upload.path);
});
response.writeHead(200, {"Content-Type": "text/html"});
response.write("received image:
");
response.write("");
response.end();
});
}
function show(response, request) {
console.log("Request handler 'show' was called.");
fs.readFile("D:\\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": "image/jpg"});
response.write(file, "binary");
response.end();
}
});
}
exports.start = start;
exports.upload = upload;
exports.show = show;
以上为代码的全部文件,由于需要用到处理上传文件的node-formidable模块,需要到命令行下进行安装。
>npm install formidable
开始时,将上传的文件重命名使用的是fs模块的renameSync方法,但由于nodejs装在C盘,临时文件上传到了D盘,出现了磁盘之间的访问权限问题,后来通过使用util模块的pump方法成功解决掉了。
注:此nodejs小程序是在Windows平台搭建的