javascript - 上传文件报错,麻烦大家帮我看看是哪里的问题◔ ‸◔?
PHPz
PHPz 2017-04-11 13:11:03
[JavaScript讨论组]

参考教程地址:http://www.nodebeginner.org/i...

才开始学 Node.js,现在出现问题,不清楚是哪里的原因,也不知道应该如何排查。麻烦大家指点一二,谢谢。

附上服务器环境

CentOS 6.8

Node.js v0.12.9

贴上我的代码(参考教程)

index.js:

var server = require('./server'),
  router = require('./router'),
  requestHandlers = require('./requestHandlers');

// URL 中请求路径与请求处理程序关联,原本在路由中通过if判断进行。现在搬到这里通过对象做对应关系的映射。
var handle = {};
handle['/'] = requestHandlers.start;
handle['/start'] = requestHandlers.start;
handle['/upload'] = requestHandlers.upload;
handle['/show'] = requestHandlers.show;

// 依赖注入:将路由函数 注入到 服务器中
// 依赖注入:将做好映射关系的对象 注入到 服务器中。
server.start(router.route, handle);

server.js

/**
 * 一个简易的 HTTP 模块
 */

// 通过 require 获取 HTTP 模块,并把它赋值给 http 变量
var http = require('http'),
  url = require('url');

// 将启动服务器的代码封装到 start 函数中。
// 依赖注入:路由函数以参数传入start。
// 依赖注入:对象以参数传入start。
function start(route, handle) {
  // 自定义一个函数,以参数的形式传入 createServer 函数中。
  function onRequest(request, response) {
    // 获取 url 中的 pathname
    var pathname = url.parse(request.url).pathname;

    route(pathname, handle, response, request);

  }

  // 调用 http 模块提供的函数:createServer
  // createServer 函数会返回一个对象,这个对象有一个 listen 方法
  // 通过调用 listen 方法并且传入一个数值参数,指定这个 HTTP 服务器监听的端口号。
  http.createServer(onRequest).listen(8888);
}
// 将封装好的 start 函数以接口的形式暴露给其他模块
exports.start = start;

console.log('Server is running...');

router.js

// 依赖注入:最后一步,对象 以参数的形式传入route
function route(pathname, handle, response, request) {
  console.log('About to route a request for ' + pathname);
  // 通过路径字符串访问对象的属性,然后调用对应的方法。
  // 总结:这种方式把路径与请求处理程序的关联同时对象实现,然后将对象一次注入到 服务器、路由中,路由中只需要直接调用对象对应属性中保存的方法即可。
  if (typeof handle[pathname] === 'function') {
    // 传递 response 给 请求处理程序
    handle[pathname](response, request);
  } else {
    response.writeHead(404, {
      'Content-Type': 'text/plain'
    });
    response.write('404 Not found');
    response.end();
  }
}
exports.route = route;

requestHandlers.js

var querystring = require('querystring'),
  fs = require('fs'),
  formidable = require('formidable');

function start(response) {
  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, '/tmp/test.png'); // 返回 upload 处理程序的处理结果。 response.writeHead(200, { 'Content-Type': 'text/html' }); response.write('received image:
'); response.write('') response.end(); }); } function show(response) { 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;

目前访问 example.com/start 正常,但是在选择好 xxx.png 文件,点击上传之后服务器就歇菜了。。。服务器中也没有接收到上传的文件。

服务器报错信息

报错对应字符版本:

Server is running...
About to route a request for /start
Request handler "start" was called.
About to route a request for /upload
Request handler "upload" was called.
about to parse
parsing done
/var/public_root/nodejs/requestHandlers.js:34
    fs.renameSync(files.upload.path, '/tmp/test.png');
                              ^
TypeError: Cannot read property 'path' of undefined
    at /var/public_root/nodejs/requestHandlers.js:34:31
    at IncomingForm. (/var/public_root/nodejs/node_modules/formidable/lib/incoming_form.js:105:9)
    at IncomingForm.emit (events.js:104:17)
    at IncomingForm._maybeEnd (/var/public_root/nodejs/node_modules/formidable/lib/incoming_form.js:553:8)
    at QuerystringParser.parser.onEnd (/var/public_root/nodejs/node_modules/formidable/lib/incoming_form.js:450:10)
    at QuerystringParser.end (/var/public_root/nodejs/node_modules/formidable/lib/querystring_parser.js:25:8)
    at IncomingMessage. (/var/public_root/nodejs/node_modules/formidable/lib/incoming_form.js:130:30)
    at IncomingMessage.emit (events.js:104:17)
    at _stream_readable.js:908:16
    at process._tickCallback (node.js:355:11)
PHPz
PHPz

学习是最好的投资!

全部回复(1)
高洛峰

已经找到问题的原因,多谢 @gwl002 。
表单中的属性拼错单词。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号