I'm breaking my head here to try to upload images with Nodejs but it's being "freud".
I am developing an api where you will have access from three domains. So the structure of my api is basically:
public
src
company
... *.js
routes.js
client
... *.js
routes.js
admin
... *.js
routes.js
routes
routes.js
app.js
In each directory destined for your respective domain, I have the route file calling your controllers. An example, the routes accessed by the company domain:
router.get('/c/:id', auth.authorize, controller.getData);
router.post('/login', controller.login);
router.post('/register', controller.create);
router.get('/activate/:code', controller.checkActivationCode);
router.put('/update/:id/:section', auth.authorize, controller.update);
router.get('/get_business_hours', auth.authorize, controller.getBusinessHours);
router.post('/logo', auth.authorize, imgUpload);
module.exports = router;
In the routes directory, I get these routes from each domain and condensa in a single file, thus:
'use strict';
const express = require('express');
const app = express();
const router = express.Router();
router.get('/', (req, res) => {
res.status(200).send({
version: "1.0.0"
});
});
const companyRoutes = require('../company/routes/company');
app.use('/', router);
app.use('/api/company/', companyRoutes);
module.exports = app;
As I will have many routes since there will be three domains accessing, subdivide this way. But here comes the problem, in the post ('/ logo') route, where the multer calls to store the image, the file is not uploaded.
In the image upload controller it looks like this:
const multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/public/')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
})
var upload = multer({ storage: storage }).single('logo')
module.exports = upload;