0
I am developing a file upload application on Node.JS. There is a snippet in the code that transfers the file to a specific folder on the server...
router.post('/', (req, res, next) => {
const formidable = require('formidable');
const fs = require('fs');
const form = new formidable.IncomingForm();
form.parse(req, (err, fields, files) => {
const path = require('path');
const oldpath = files.filetoupload.path;
const newpath = path.join(__dirname, '..', files.filetoupload.name);
fs.renameSync(oldpath, newpath);
res.send('File uploaded and moved!');
});
});
What is the difference between using Fs.renameSync() and Fs.Rename()? Performing Google search I saw that I can use the Fs.copyFile() function that has the same result. So... I’m in doubt as to how best to use?
Follow the upload part code:
var express = require('express');
var router = express.Router();
router.get('/', function (req, res, next) {
res.render('index', { title: 'Express' });
});
router.post('/', (req, res, next) => {
const formidable = require('formidable');
const fs = require('fs');
const form = new formidable.IncomingForm()
form.parse(req, (err, fields, files) => {
const path = require('path');
const oldpath = files.filetoupload.path;
const newpath = path.join(__dirname, '..', files.filetoupload.name);
fs.copyFile(oldpath, newpath, (err) => {
if (err) return console.log(err);
res.send('File Uploaded and Moved!')
});
});
});
module.exports = router;