upload files to remote server using multer sftp in express Node js? - javascript

I'm trying to upload the files to remote server using multer-sftp in node js. since i'm following the official docs npm multer-sftp. Previously i've uploading the files to Amazon S3 instead of remote server. now i want to upload the files to remote server.
API:
exports.newFileUpload = function(req , res , next){
var storage = sftpStorage({
sftp: {
host: 'http://www.port*****es.in/',
port: 22,
username: 'username',
password: 'password'
},
destination: function (req, file, cb) {
cb(null, 'images/')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
})
var upload = multer({ storage: storage }).array('file');
upload(req,res,function(err){
logger.debug(JSON.stringify(req.body));
logger.debug(JSON.stringify(req.files));
if(err){
logger.debug("Error Occured", JSON.stringify(err));
res.json({error_code:1,err_desc:err});
return;
} else{
res.json({error_code:0,err_desc:null});
}
});
}
While uploading the file, returning the error
2017-11-10T02:39:48.297Z - debug: Error Occured {"code":"ENOTFOUND","errno":"ENOTFOUND",
"syscall":"getaddrinfo","hostname":"http://www.port****es.in/","host":"http://www.port****es.in/",
"port":22,"level":"client-socket","storageErrors":[]}
And also port no 22 is open in my domain. Awaiting Suggestions,
Thanks in Advance.

For Your Error, there are two possibilities
port no 22 is not open state, also not able to access that folder
Check your folder directory in domain
Uploading Files to remote server using multer-sftp is easy and flexible way.
also we can upload the files to remote server with scp, ssh techniques in node js.
Working Code:
exports.newFileUpload = function(req , res , next){
var storage = sftpStorage({
sftp: {
host: 'hostname',
port: 22,
username: 'username',
password: 'password'
},
destination: function (req, file, cb) {
cb(null, 'images/')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
})
var upload = multer({ storage: storage }).array('file');
upload(req,res,function(err){
logger.debug(JSON.stringify(req.body));
logger.debug(JSON.stringify(req.files));
if(err){
logger.debug("Error Occured", JSON.stringify(err));
res.json({error_code:1,err_desc:err});
} else{
logger.debug("Files uploaded successfully");
res.json({error_code:0,err_desc:null});
}
});
}
Note: When using 'multer-sftp' port no 22 is open in remote server.
Hope it helps !

Related

Error: ENOENT: no such file or directory Nodejs

I'm trying to upload a file and store it in an uploads folder, but I get this error: no such file or directory
I get the message success in console but I get this error anyway.
POST /auth/register 500 21.023 ms - 260
Error: ENOENT: no such file or directory, open E:\IMPORTANT\INFO-DEV\DEV\ANGULAR NODEJS\API AUTH\uploads\1671534381494.jpeg
Here is my configuration code for upload.
const path = require("path");
const multer = require("multer");
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "uploads/");
},
filename: function (req, file, cb) {
const extension = path.extname(file.originalname);
cb(null, Date.now() + extension);
},
});
const upload = multer({
storage: storage,
fileFilter: function (req, file, callback) {
if (
file.mimetype == "image/png" ||
file.mimetype == "image/jpg" ||
file.mimetype == "image/jpeg"
) {
callback(null, true);
console.log("Image téléchargé avec succès"); // success message
} else {
callback(null, false);
console.log("Seulement du fichier de type png, jpg ou jpeg"); // error message
}
},
limits: {
fileSize: 1024 * 1024 * 2,
},
});
module.exports = upload;
I got the same error, but I was able to solve it by getting my current path.
import multer from "multer";
// Set up multer storage options
const storage = multer.diskStorage({
destination: function (req, file, cb) {
console.log("🚀 ~ file: upload.ts:11 ~ file", process.cwd());
cb(null, `${process.cwd()}/src/Images`);
},
filename: function (req, file, cb) {
cb(null, file.fieldname + "-" + Date.now());
},
});
// Create a multer instance with the storage options
const upload = multer({ storage });
export default upload;
The issue is that your uploads folder doesn't exist or you set the path incorrectly.
I don't know where exactly you created uploads folder (and if you created it at all).
So in the destination param you should pass either:
path.join(__dirname, '/uploads') - in case that folder is in the same location where current js file is located.
Or path.join(process.cwd(), '/uploads') - in case if uploads folder is in the root of the project (where you run npm start etc.)
So, in short words you need to make sure folder exists and then make sure the path is correct.
P.S.
Using ../../ syntax should also work, you can try ../uploads or ../../uploads if, for example, that folder is on higher levels of your folders structure.
Change this line
cb(null, './uploads/');
this line
cb(null, "uploads/");
or try
cb(__dirname, "uploads/");
you can try your path name as like
img: {
data: fs.readFileSync(
path.join(__dirname + "../../uploads" + req.file.filename)
),
contentType: "image/png",
},

Multer in Nodejs using Express Router req.file undefined

I am trying to implement File Upload functionality using multer and Express Router. I defined an endpoint /batch_upload using router.use like below
api.js
router.use(
"/batch_upload",
upload.single("emp_csv_data"),
userController.processBatchUserInformation
);
in userController.js
exports.processBatchUserInformation = async (req, res) => {
console.log(req.file);
if (req.method == "POST") {
try {
console.log("Upload route reached - POST");
console.log(req.file);
console.log(req.file.path);
return res.send(req.file);
} catch (err) {
console.log("Error Occurred");
return res.send(err);
}
}
};
In the FileUploader.js, I defined the upload variable and multer options like below
var multer = require("multer");
var storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "uploads");
},
filename: (req, file, cb) => {
return cb(null, file.fieldname + "-" + Date.now());
}
});
exports.upload = multer({ storage: storage });
Finally, in the app.js I used the route using
app.use('/user',user_routes)
But when I send a file to http://localhost:5000/user/batch_upload, I get an undefined response for req.file
Irony is that I have the exact implementation in a sample test project and everything seems fine. I don't understand what am I missing. If you see something that seems off, please help me fix it.
So, the reason behind the file not being uploaded was that I did not add Content-type:multipart/form-data in the headers. Thank you guys for trying to help. I appreciate it.

Sending file through HTTP request

I tried to receive the file and store it in the multer storage
Node js code
enter code here
app.post('/createLicence', upload.single('photo'),function(req, res ,next) {
// any logic goes here
console.log("filename" ,req.body.name)
if (!req.file) {
console.log("No file received");
return res.send({
success: false
});
} else {
console.log('file received');
var function_name = 'createLicence'
var arguments_array = [req.file.path,'Raghav','Mumbai','Approved']
invoke = require('/Users/sanjeev.natarajan/fabric-samples/fabcar/invoke.js');
invoke.invokechaincode(function_name,arguments_array)
return res.send({
success: true
})
}
});
but i am receiving no file is receivedi have send the request through postman
-
From : https://www.npmjs.com/package/multer
In order to use the multer package, you have first to define a few parameters so that it can work on your fileDirectory.
In your server.js :
let multer = require('multer');
let storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, '/path/to/storage/')
},
filename: function(req, file, callback) {
callback(null, file.originalname + '-' + Date.now());
}
});
let upload = multer({
storage: storage
});
Now, configure your route
router.route('/your/payload')
.post(authController.isAuthenticated, upload.any(), albumController.postFile)
Note that upload.any() will allow you to upload multiple different formatted files at once. Feel free to use any other kind of upload.method() depending on your needs.
From this point, multer already is doing its job, however you might want to keep track of the files uploaded on your server.
So, in your own module, the logic is pretty much straight forward :
(I'm assuming that you're using mongoose models since you're not giving much information, but that's not the relevant part anyway)
exports.postFile = async (req, res) => {
if (!req || !req.files || !req.files[0]) return res.status(400).send("Bad request.");
for (let i = 0; req.files[i]; i++) {
await File.create({
path: req.files[i],
originalName: req.files[i].originalName,
mimetype: req.files[i].mimetype,
owner: req.user.userId
}, (err, file) => {
if (err) console.log("Something went wrong: " + err); else {
// Do something with file
}
});
}
return res.status(418).send("I'm a teapot.");
}
This configuration and middleware use is ONLY for testing purpose, never ever let anyone upload something to your server without carefully handle that uploading process (file integrity, resource management, ...). An open uploading system can become a very wide backdoor getting straight to your server.
Hope this helps,
regards.

how to copy files to remote server using scp2 in node js?

I want to copy the files from local server to remote server in Node js using scp2 package. First of all.. Files uploaded to local server using multer after that copy or move that files to remote server.
My Code:
exports.newFileUpload = function(req , res , next){
var storage = multer.diskStorage({ //multers disk storage settings
destination: function (req, file, cb) {
cb(null, 'uploads/');
},
filename: function (req, file, cb) {
var datetimestamp = Date.now();
cb(null, datetimestamp+ '-' +file.originalname);
}
});
var upload = multer({ storage: storage, limits: { fieldSize: 25 * 1024 * 1024 }}).array('file');
upload(req,res,function(err){
console.log(req.body);
console.log(req.files);
if(err){
res.json({error_code:1,err_desc:err});
console.log("Error Occured", err);
return;
}else{
client.scp(req.files[0].path, {
host: 'www.do********.in',
username: 'username',
password: '*********',
path: '/uploads/'
}, function(err) {
console.log(req.files[0].path);
console.log("files uploaded in remote server");
res.json({error_code:0,err_desc:null});
});
}
});
}
file upload to local server is perfectly working, after that to remote server throwing error
Error:
{ date: 'Mon Nov 13 2017 01:00:22 GMT+0530 (India Standard Time)',
process:
{ pid: 5664,
uid: null,
gid: null,
cwd: 'D:\\sample',
execPath: 'C:\\Program Files\\nodejs\\node.exe',
version: 'v8.2.1',
argv: [ 'C:\\Program Files\\nodejs\\node.exe', 'D:\\sample\\app.js' ],
memoryUsage:
{ rss: 69619712,
heapTotal: 45162496,
heapUsed: 39166256,
external: 149849 } },
os: { loadavg: [ 0, 0, 0 ], uptime: 3537.1088452 },
trace:
[ { column: 11,
file: 'util.js',
function: 'Object.exports._errnoException',
line: 1024,
method: '_errnoException',
native: false },
{ column: 20,
file: 'util.js',
function: 'exports._exceptionWithHostPort',
line: 1047,
method: '_exceptionWithHostPort',
native: false },
{ column: 14,
file: 'net.js',
function: 'TCPConnectWrap.afterConnect [as oncomplete]',
line: 1150,
method: 'afterConnect [as oncomplete]',
native: false } ],
stack:[ 'Error: Can\'t set headers after they are sent.',
' at validateHeader (_http_outgoing.js:504:11)',
' at ServerResponse.setHeader (_http_outgoing.js:511:3)',
' at ServerResponse.header (D:\\sample\\node_modules\\express\\lib\\response.js:730:10)',
' at ServerResponse.send (D:\\sample\\node_modules\\express\\lib\\response.js:170:12)',
' at ServerResponse.json (D:\\sample\\node_modules\\express\\lib\\response.js:256:15)',
' at D:\\sample\\routes\\part.js:302:10',
' at Client.closeHandler (D:\\sample\\node_modules\\scp2\\lib\\scp.js:48:13)',
' at emitNone (events.js:105:13)',
' at Client.emit (events.js:207:7)',
' at Client.<anonymous> (D:\\sample\\node_modules\\scp2\\lib\\client.js:88:10)',
' at emitNone (events.js:105:13)',
' at Client.emit (events.js:207:7)',
' at Socket.<anonymous> (D:\\sample\\node_modules\\ssh2\\lib\\client.js:225:10)',
' at emitOne (events.js:115:13)',
' at Socket.emit (events.js:210:7)',
' at Pipe._handle.close [as _onclose] (net.js:549:12)' ] }
Couldn't able to identify the error, awaiting suggestions or possible ways to solve the problem.
Thanks in Advance !
Uploading Files to remote server using multer is not possible directly, But we can play around with multer-sftp, scp, ssh techniques Methodologies
When Uploading Files to remote server using Node Js, We need to take care few things
Username and password should be correct
The Corresponding port number should be open in remote server
The Remote Directory Should have Write Access
Working Code using scp2 to move the files to remote server:
exports.newFileUpload = function(req , res , next){
var storage = multer.diskStorage({ //multers disk storage settings
destination: function (req, file, cb) {
cb(null, 'uploads/');
},
filename: function (req, file, cb) {
var datetimestamp = Date.now();
cb(null, datetimestamp+ '-' +file.originalname);
}
});
var upload = multer({ storage: storage, limits: { fieldSize: 25 * 1024 * 1024 }}).array('file');
upload(req,res,function(err){
console.log(req.body);
console.log(req.files);
if(err){
res.json({error_code:1,err_desc:err});
console.log("Error Occured", err);
return;
}else{
client.scp(req.files[0].path, {
host: 'host',
username: 'username',
password: '*********',
path: '/uploads/'
}, function(err) {
console.log(req.files[0].path);
console.log("files uploaded in remote server");
});
}
});
}
Note: Should install required packages as well as declare require things in code.
References: multer-sftp, scp2 , node-ssh

multer file-upload doesn't work with node-windows

I am trying to upload picture files, using multer.
This is is my code:
router.post('/', function (req, res) {
upload(req, res, function (err) {
if (err) {
// An error occurred when uploading
conole.log(err);
return;
}
// Everything went fine
var data = Object.assign({picture: req.file && req.file.filename}, req.body);
db.create('devices', data)
.then(function(data){
res.render('msg', {msg: 'Added device '+ data[0]});
})
.catch(function(err){
console.log(err);
});
});
});
However, when I try to run the server from the command-line node app.js, everything is working as expected. But when I'm running it from A windows service, using node-windows, it doesn't seem to work, while I don't get any errors. (Meaning the picture file name is actually recorded in the database, but the file doesn't upload.
is it maybe a destination issue? if so, I will provide my destination code:
var storage = multer.diskStorage({
destination: './public/uploads/',
filename: function (req, file, cb) {
cb(null, file.originalname + "-" + Date.now() + path.extname(file.originalname));
}
});
remove this line from your code
destination: './public/uploads/',
and add this code
destination: __dirname+'../../../uploads',
this will work both mac OS and windows

Categories