0
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 assigned to 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 take these routes from each domain and connect in a single file, thus staying:
'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 it will be three domains accessing, I subdivided this way. But now that the problem comes, in the post('/logo') route, where the multer calls to store the image, the file is not uploaded.
The image upload file (controller) 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;