1

The node.js server code below allows the client to upload file to a temporary location.

var restify = require('restify');
var server = restify.createServer();
server.use(restify.bodyParser());

server.post('/fileupload', function(req, res, next){
    var path_temp = req.files.file.path;
    console.log(path_temp);
    res.end('upload');

    next();
});

server.listen(8000);

The uploaded file is stored at the folder location path_temp. How can one copy this file to the current folder of the node.js script being run?

guagay_wk
  • 26,337
  • 54
  • 186
  • 295
  • Possible duplicate of [Upload files with node.js](http://stackoverflow.com/questions/23263307/upload-files-with-node-js) – Shanoor Nov 25 '15 at 11:35
  • process.cwd() will give you the current working directory of the node script. Checkout fs.copy – Dave Briand Nov 25 '15 at 11:36

1 Answers1

1

Thanks to hints provided in the comments, here is the answer for my own question.

var restify = require('restify');
var fs = require('fs-extra');
var server = restify.createServer();
server.use(restify.bodyParser());

server.post('/fileupload', function(req, res, next){
    var path_temp = req.files.file.path; 
    var currentFolder = process.cwd(); 
    var filename = 'filename.txt'; //up to you
    fs.move(path_temp, currentFolder + '/' + filename, function(err) {
        if (err) return console.error(err)
        console.log("file uploaded!")
    });

    res.end('upload');

    next();
});

server.listen(8000);
guagay_wk
  • 26,337
  • 54
  • 186
  • 295