带有上传文件URL的node.js强大的make变量

时间:2018-09-17 20:23:51

标签: node.js ibm-watson formidable

您好,我使用节点,并且难以提交表单文件,该文件的URL需要保存在全局变量中,以便以后与WATSON IBM图像识别API结合使用。

我是node的新手,所以我被卡住了,变量名是newpath,在提交表单后我可以打印它,但是以后不能访问该变量。

我一定做错了,如果您能指出我的错误,我将不胜感激。

const http = require('http');
var formidable = require('formidable');

const hostname = '127.0.0.1';
const port = 3500;

var fs = require('fs');

/// WATSON

var VisualRecognitionV3 = require('watson-developer-cloud/visual-recognition/v3');
var visualRecognition = new VisualRecognitionV3({
  version: '2018-03-19',
  iam_apikey: 'xxxxxxx'
});

// SERVER AND FORM

const server = http.createServer((req, res) => {

  if (req.url == '/fileupload') {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
      var oldpath = files.filetoupload.path;
      var newpath = '/users/myuser/coding/visualr/' + files.filetoupload.name;

      fs.rename(oldpath, newpath, function (err) {
        if (err) throw err;
        res.write('File uploaded and moved!');
        // this is the path, variable newpath, but can't be accessed
        // outside this function, tried to make it global but didn't work either

        res.write('newpath');
        res.end();
      });


 });

  } else {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
    res.write('<input type="file" name="filetoupload"><br>');
    res.write('<input type="submit">');
    res.write('</form>');
    return res.end();
  }
  });


 var images_file = fs.createReadStream(newpath);
// I want to put the variable newpath in this function: but it doesn't work...

var params = {
  images_file: images_file,
};

visualRecognition.classify(params, function(err, response) {
  if (err)
    console.log(err);
  else
    console.log(JSON.stringify(response, null, 2))
});


// ENDS

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

1 个答案:

答案 0 :(得分:0)

该变量是在if (req.url == '/fileupload') {...}块的上下文中定义的,因此在该块之外将不可用。

要在代码中的任何位置使用变量,请在createServer块之外定义变量:

var newpath; // this variable will have global context for this file

const server = http.createServer((req, res) => {

  if (req.url == '/fileupload') {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
      var oldpath = files.filetoupload.path;

      // set the variable to its intended value here
      newpath = '/users/myuser/coding/visualr/' + files.filetoupload.name;

      fs.rename(oldpath, newpath, function (err) {
        if (err) throw err;
        res.write('File uploaded and moved!');
        res.write('newpath');
        res.end();
      });
    });
  } else {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
    res.write('<input type="file" name="filetoupload"><br>');
    res.write('<input type="submit">');
    res.write('</form>');
    return res.end();
  }
});

console.log(newpath); // the variable should be available here, because of its context