博客
关于我
强烈建议你试试无所不能的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

你可能感兴趣的文章
迷人的卡耐基说话术
查看>>
PHP导出table为xls出现乱码解决方法
查看>>
PHP问题 —— 丢失SESSION
查看>>
Java中Object类的equals()和hashCode()方法深入解析
查看>>
数据库
查看>>
Vue------第二天(计算属性、侦听器、绑定Class、绑定Style)
查看>>
dojo.mixin(混合进)、dojo.extend、dojo.declare
查看>>
Python 数据类型
查看>>
iOS--环信集成并修改头像和昵称(需要自己的服务器)
查看>>
PHP版微信权限验证配置,音频文件下载,FFmpeg转码,上传OSS和删除转存服务器本地文件...
查看>>
教程前言 - 回归宣言
查看>>
PHP 7.1是否支持操作符重载?
查看>>
Vue.js 中v-for和v-if一起使用,来判断select中的option为选中项
查看>>
Java中AES加密解密以及签名校验
查看>>
定义内部类 继承 AsyncTask 来实现异步网络请求
查看>>
VC中怎么读取.txt文件
查看>>
如何清理mac系统垃圾
查看>>
企业中最佳虚拟机软件应用程序—Parallels Deskto
查看>>
Nginx配置文件详细说明
查看>>
怎么用Navicat Premium图标编辑器创建表
查看>>