【问题标题】:Write a remote txt file using Nodejs使用 Nodejs 编写远程 txt 文件
【发布时间】:2015-09-24 23:31:36
【问题描述】:

我在服务器端使用 NodeJs,但我需要在文本文件中重写(擦除它的内容并重新写入)或在不使用 ftp、Web 服务器或其他东西的情况下将其发送到同一网络中的 RaspberryPi在覆盆子里。

我一直在阅读有关 NodeJs 的 'request' 的信息,但我认为我需要在 Raspberry 中安装类似 Web 服务器的东西才能对 Raspberry 中的某个 URL 执行 'post'

   function uploadFile() {
        var formData = {
            // Pass a simple key-value pair
            my_field: 'my_value',
            // Pass data via Buffers
            my_buffer: new Buffer([1, 2, 3]),
            // Pass data via Streams
            my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
            // Pass multiple values /w an Array
            attachments: [
                fs.createReadStream(__dirname + 'porton/json.txt')
            ],
            // Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS}
            // Use case: for some types of streams, you'll need to provide "file"-related information manually.
            // See the `form-data` README for more information about options: https://github.com/felixge/node-form-data
            custom_file: {
                value: fs.createReadStream('/dev/urandom'),
                options: {
                    filename: 'topsecret.jpg',
                    contentType: 'image/jpg'
                }
            }
        };
        request.post({ url: '<RaspberryStaticIP>/route/message.txt', formData: formData }, function optionalCallback(err, httpResponse, body) {
            if (err) {
                return console.error('upload failed:', err);
            }
            console.log('Upload successful!  Server responded with:', body);
        });    
    }

https://github.com/request/request#multipartform-data-multipart-form-uploads

使用curl 进行多部分上传

    curl('127.0.0.1/upload.php', {
    MULTIPART: [
        {name: 'file', file: '/file/path', type: 'text/html'},
        {name: 'sumbit', contents: 'send'}
    ]
}, function(e) {
    console.log(e);
    console.log(this.body);
    this.close()
});

https://www.npmjs.com/package/node-curl#multipart-upload

或者,是否可以使用'fs' 但无需本地化?

fs.writeFile('<RaspberryStaticIP>/route/message.txt', '123', function (err) {
  if (err) throw err;
  console.log('It\'s saved!'); 
}); 

是否有另一种方法可以仅使用 NodeJs(及其任何实现)或 PHP 来做到这一点?如果无法按照我说的方式进行操作,那么最好的方法是通过 FTP、Raspberry 中的 WebServer 或任何推荐方法进行操作。

【问题讨论】:

    标签: php node.js curl ftp raspberry-pi


    【解决方案1】:

    有很多方法可以将文件放在服务器上,我假设您在本地网络上执行此操作(因为您使用的是 RPi)并且并不完全需要使用 NodeJS,因为您暗示PHP 等。这里只是其中之一,使用了一个 shell 脚本和一个 Here 文档:

    http://www.stratigery.com/scripting.ftp.html

    【讨论】:

    • 我猜这是给树莓派的,但实际上我需要将文件从服务器发送到树莓派。或者你是说我可以在 Raspberry 中安装它并远程获取文件?
    【解决方案2】:

    我认为您需要使用 SSH 登录到目标服务器,然后执行您的文件写入命令。

    没有尝试过,但也许这会有所帮助。

    https://github.com/mscdex/ssh2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-07
      相关资源
      最近更新 更多