博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nodejs初学示例
阅读量:6258 次
发布时间:2019-06-22

本文共 3543 字,大约阅读时间需要 11 分钟。

hot3.png

最近学习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;

转载于:https://my.oschina.net/robinjiang/blog/168737

你可能感兴趣的文章
实战操作主机角色转移之清除宕机DC的元数据(三)
查看>>
MySQL实现序列(Sequence)效果以及在Mybatis中如何使用这种策略
查看>>
QTP关键字视图下显示项的相关设置
查看>>
openDICOM
查看>>
Linux下有两种聊天命令
查看>>
DataGridView 行的用户删除操作的自定义
查看>>
linux cpu内存利用率获取
查看>>
产品设计体会(8009)产品经理值得看的16个博客
查看>>
Hyper-V 2016 系列教程13 虚拟机监控程序规范
查看>>
SetupDiGetDeviceInterfaceDetail 函数
查看>>
让百度、Google搜到你的博客和论坛
查看>>
C++串口编程实例
查看>>
SSRS 2012 报表基本结构与设置
查看>>
Exchange 2013部署系列之(七)配置SSL多域名证书
查看>>
WPF:从WPF Diagram Designer Part 1学习控件模板、移动、改变大小和旋转
查看>>
创建与SharePoint 2010风格一致的下拉菜单
查看>>
Linux下创建与解压zip, tar, tar.gz和tar.bz2文件
查看>>
IT基础结构-4.BDNS-安装与配置
查看>>
轮番上阵:Linux下查找漏洞的N种兵器(转贴)
查看>>
综合应用WPF/WCF/WF/LINQ之六:数据库结构
查看>>