最近学习nodejs,参考文章:。
其中在最后一个实例中,按照教程编写代码会出现以下错误:
Error: EXDEV, cross-device link not permitted 'C:\Users\DONGQI~1\AppData\Local\Temp\726b6c28afd4cc33c2644e203756d4cd' at Object.fs.renameSync (fs.js:543:18) at F:\nodejsSpace\requestHandlers.js:34:8 at IncomingForm.(F:\nodejsSpace\node_modules\formidable\lib\incoming_form.js:92:9) at IncomingForm.EventEmitter.emit (events.js:92:17) at IncomingForm._maybeEnd (F:\nodejsSpace\node_modules\formidable\lib\incoming_form.js:533:8) at F:\nodejsSpace\node_modules\formidable\lib\incoming_form.js:211:12 at WriteStream. (F:\nodejsSpace\node_modules\formidable\lib\file.js:70:5) at WriteStream.g (events.js:175:14) at WriteStream.EventEmitter.emit (events.js:117:20) at finishMaybe (_stream_writable.js:354:12)
查找资料说是windows下没有相关权限导致,只需要在var form = new formidable.IncomingForm();后加一句:form.uploadDir = 'tmp' 改变上传目录的地址就可以了。
以下为示例代码:
1、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;2、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);3、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;4、requestHandlers.js
var querystring = require("querystring"), fs = require("fs"), formidable = require("formidable");function start(response, postData) { 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(); form.uploadDir = 'tmp'; 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:"); response.write(""); response.end(); });}function show(response, postData) { console.log("Request handler 'show' was called."); fs.readFile("./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;