使用Nodejs编写远程txt文件

Write a remote txt file using Nodejs

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

我一直在阅读有关 NodeJs 的 'request' 但我认为我需要在 Raspberry 中安装类似 Web 服务器的东西来对 'post' 中的某些 URL覆盆子。

   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 或任何推荐方式进行操作..

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

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

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

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

https://github.com/mscdex/ssh2