Reputation: 71
I'm new in nodejs, and during the test of my post method that upload image from the computer I get in postman this message that shows that my upload method doesn't exist
my server.js code is
const express = require('express');
const multer = require('multer');
const app = express();
//moddleware
app.use(express.urlencoded({extended: true}));
app.use(multer.json(''));
const PORT = process.env.PORT | 5000;
var Storage = multer.diskStorage({
destination: (req, file, callback) => {
callback(null,"/images");
},
filename: (req, file, callback) => {
callback(null,file.fieldname);
}
});
var upload = multer({
storage: Storage
}).array('image',3);
//route
app.post('/', (req, res) => {});
app.post('/upload', (req, res) => {
upload(req, res , err => {
if (err) {
return res.send('somthing went wrong');
}
return.res.send('file uploaded successfully');
});
});
app.listen(PORT, () => {
console.log('Server running on PORT ${PORT}')
});
and this is my server response
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
Server running on PORT ${PORT}
thank you
Upvotes: 2
Views: 5755
Reputation: 71
I finally made it here is the code
// const bodyParser= require('body-parser');
const multer = require('multer');
const app = express();
// app.use(bodyParser.urlencoded({extended: true}))
const PORT = process.env.PORT | 5000;
var Storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, 'images')
},
filename: function (req, file, callback) {
callback(null, file.orignalname);
}
});
var upload = multer({
storage: Storage
}).array('image', 3);
//route
app.post('/', (req, res) => {});
app.post('/upload', (req, res) => {
console.log(req.file);
upload(req, res , err => {
if (err) {
console.log(err);
return res.send('somthing went wrong');
}
return res.send('file uploaded successfully');
});
});
app.listen(PORT, () => {
console.log('Server running on PORT ${PORT}')
});
thank you for helping me
Upvotes: 1
Reputation: 261
your code has some error inside the upload route return.res.send which is causing the issue. make changes as follow.
const express = require('express')
const bodyParser= require('body-parser')
const multer = require('multer');
const app = express();
app.use(bodyParser.urlencoded({extended: true}))
const PORT = process.env.PORT | 5000;
var Storage = multer.diskStorage({
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('image', 12)
//route
app.post('/', (req, res) => {});
app.post('/upload', (req, res) => {
upload(req, res , err => {
if (err) {
res.send('somthing went wrong');
}
res.send('file uploaded successfully');
});
});
app.listen(PORT, () => {
console.log('Server running on PORT ${PORT}')
});
Upvotes: 1