從零開始學(xué)習(xí)Node.js系列教程三:圖片上傳和顯示方法示例
本文實(shí)例講述了Node.js圖片上傳和顯示方法。分享給大家供大家參考,具體如下:
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) {
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(3000);
console.log("Server has started.");
}
exports.start = start;
requestHandlers.js
var querystring = require("querystring"),
fs = require("fs"),
formidable = require("formidable");
function start(response) {
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 = "D:\\min\\nodejsExample2\\tmp";
console.log("about to parse1");
form.parse(request, function(error, fields, files) {
console.log("parsing done");
console.log(files.upload.path);
fs.renameSync(files.upload.path, "D:\\min\\nodejsExample2\\tmp\\test.png");
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("D:\\min\\nodejsExample2\\tmp\\test.png", "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/png"});
response.write(file, "binary");
response.end();
}
});
}
exports.start = start;
exports.upload = upload;
exports.show = show;
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;
result:



知識(shí)點(diǎn):
其中用到了fs模塊的readFile讀取文件,它有同步和異步兩個(gè)版本。node.js中,并不是所有的API都提供了異步和同步版本,node.js不鼓勵(lì)使用同步I/O。
//this is async 異步
/*
fs.readFile調(diào)用時(shí)所做的工作只是將異步式I/O請(qǐng)求發(fā)送給了操作系統(tǒng),然后立即返回并執(zhí)行后面的語(yǔ)句,執(zhí)行完以后進(jìn)入事件循環(huán)監(jiān)聽事件。
當(dāng)fs接收到I/O請(qǐng)求完成的事件時(shí),事件循環(huán)會(huì)主動(dòng)調(diào)用回調(diào)函數(shù)以完成后續(xù)工作。
*/
var fs = require('fs');
fs.readFile('file.txt', 'utf-8', function(err, data){
if (err){
console.error(err);
} else {
console.log(data);
}
});
//this is sync 同步
var fs = require('fs');
var data = fs.readFileSync('file.txt', 'utf-8');
console.log(data);
console.log('end.');
希望本文所述對(duì)大家nodejs程序設(shè)計(jì)有所幫助。
相關(guān)文章
express中創(chuàng)建 websocket 接口及問(wèn)題解答
本文主要介紹了express中創(chuàng)建 websocket 接口及問(wèn)題解答,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
node.js中 cluster 模塊和 worker_threads 模塊示例
這篇文章主要介紹了node.js中 cluster 模塊和 worker_threads 模塊,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05
node.js報(bào)錯(cuò):Cannot find module ''ejs''的解決辦法
最近發(fā)現(xiàn)了node.js居然報(bào)錯(cuò)了,錯(cuò)誤提示為:Cannot find module 'ejs',后來(lái)找了找資料發(fā)現(xiàn)解決的方法其實(shí)很簡(jiǎn)單,下面通過(guò)這篇文章來(lái)一起看看吧,希望對(duì)同樣遇到這個(gè)問(wèn)題的朋友們能有所幫助。2016-12-12
ChatGPT編程秀之最小元素的設(shè)計(jì)示例詳解
這篇文章主要為大家介紹了ChatGPT編程秀之最小元素的設(shè)計(jì)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Node.js和Vue的安裝與配置超詳細(xì)步驟(推薦)
使用VUE前端框架開發(fā),需要安裝Node.js和Vue.js,這篇文章主要給大家介紹了關(guān)于Node.js和Vue的安裝與配置超詳細(xì)步驟的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01

