Devrath Devrath - 4 months ago 8
Node.js Question

Storing the location of the images in the database using `VARCHAR` in ExpressJS & image in server Hard-Disk

How I can do this in ExpressJS?




  • Storing the location of the images in the database using
    VARCHAR

    datatype instead of any
    BLOB
    or other
    binary
    datatype.

  • then store that image in server
    harddisk
    in possible image space
    /public/images/






i am new to express
.

Any sample example to achieve this ?

Answer

What you can do is use the multipart middleware, which automatically streams the file uploads to the disk, and then provides a req.files object.

app.use('/upload', express.multipart({ uploadDir: '/public/images' }));

Once you've configured this, in your request handler, you can get the upload path (see multiparty's documentation, which is used internally by the multipart middleware):

app.post('/upload', function (req, res, next) {
  // Assuming the field name of the file in the form is 'file'
  var path = req.files.file.path;
  connection.query('your mysql query', next);
});