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?

2
  • Possible duplicate of Upload files with node.js Commented Nov 25, 2015 at 11:35
  • process.cwd() will give you the current working directory of the node script. Checkout fs.copy Commented Nov 25, 2015 at 11:36

1 Answer 1

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); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.