Upload File Nodejs

Asked

Viewed 406 times

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;

1 answer

2


beauty?

It is because the diskStorage uses the physical address of the system to store the files on disk in the destination option. That is, if you put only one bar before the string in the Destination it will consider that you want to store the file at the root of your system. For example, if you are running your localhost on a Windows and pass cb(null, '/public/') or cb(null, '/public') (public-bar) it will try to record on the drive C:. Specifically in C: public

So the options you can use are:

  • cb(null, './public') (public-bar point)
  • cb(null, 'public')
  • cb(null, 'public/')

All these options consider that the root directory will be relative to the project folder that is running on your local server. So if your localhost is relative to the folder C: Site Projects the woman will save in C: Public Site Projects even if he is called in C: Projects Site src company Routes company.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.