NodeJS/Express – RestAPI to Upload Multipart File to MySQL using Multer + Sequelize ORM

In the tutorial, we show how to build a NodeJS/Express RestAPI to upload Multipart Files/Images to MySQL using Multer middleware and Sequelize ORM.

Related posts:
NodeJS – Save File/Image to MySQL by Sequelize with BLOB type
Multer – Build RestAPI to upload a MultipartFile to NodeJS/Express

Goal

Prerequisites

NodeJS – Save File/Image to MySQL by Sequelize with BLOB type
Multer – Build RestAPI to upload a MultipartFile to NodeJS/Express

Objective

In the tutorial, we create a NodeJS project that build RestAPI to upload files/images to MySQL database, see below project structure:

Using PostMan to make a POST request:

NodeJS-Express-Multer-Upload-MultipartFile-MySQL-Sequelize-upload-from-postman

-> result:

NodeJS-Express-Multer-Upload-MultipartFile-MySQL-Sequelize-saving-to-mysql table

Practice

Setting up NodeJS/Express project

Create a folder ‘NodeJS-Express-Multer-Upload-MultipartFile-Sequelize-MySQL’:

Then init NodeJS project:

-> Install Express, Multer, Sequelize, MySQL:

-> See package.json file:

NodeJS Application
Sequelize Image model

Create ./app/models/img.model.js file:

Multer Upload

– Create ./app/config/upload.config.js file:

Configure Sequelize MySQL Connection

– Config file ./app/config/env.js:

– Setup Sequelize-MySQL connection in ./app/config/db.config.js file:

Upload Router

./app/routers/upload.router.js file:

Upload Controller

./app/controllers/upload.controller.js file:

Server.js

server.js file:

Run & Check results

Start NodeJS server:

Make a POST request:

NodeJS-Express-Multer-Upload-MultipartFile-MySQL-Sequelize-upload-from-postman

-> Server Logs:

-> Uploaded File:

NodeJS-Express-Multer-Upload-MultipartFile-MySQL-Sequelize-saving-to-diskstorage

-> MySQL table:

NodeJS-Express-Multer-Upload-MultipartFile-MySQL-Sequelize-saving-to-mysql table

-> Load file from MySQL:

NodeJS-Express-Multer-Upload-MultipartFile-MySQL-Sequelize-saving-to-load-from-mysql

Sourcecode

To run below sourcecode, follow the guides:


step 0: download & extract zip file -> we have a folder ‘NodeJS-Express-Multer-Upload-MultipartFile-Sequelize-MySQL’
step 1: cd NodeJS-Express-Multer-Upload-MultipartFile-Sequelize-MySQL
step 2: npm install express multer sequelize mysql2 –save
step 3: node server.js
step 4: makes a POST request: http://localhost:8081/api/uploadfile

-> Sourcecode:

NodeJS-Express-Multer-Upload-MultipartFile-Sequelize-MySQL

By grokonez | May 16, 2018.



Related Posts


3 thoughts on “NodeJS/Express – RestAPI to Upload Multipart File to MySQL using Multer + Sequelize ORM”

  1. MulterError: Unexpected field
    at wrappedFileFilter (C:\ProjetoNode\NEAJB-MySQL\node_modules\multer\index.js:40:19)
    at Busboy. (C:\ProjetoNode\NEAJB-MySQL\node_modules\multer\lib\make-middleware.js:114:7)
    at emitMany (events.js:147:13)
    at Busboy.emit (events.js:224:7)
    at Busboy.emit (C:\ProjetoNode\NEAJB-MySQL\node_modules\busboy\lib\main.js:38:33)
    at PartStream. (C:\ProjetoNode\NEAJB-MySQL\node_modules\busboy\lib\types\multipart.js:213:13)
    at emitOne (events.js:116:13)
    at PartStream.emit (events.js:211:7)
    at HeaderParser. (C:\ProjetoNode\NEAJB-MySQL\node_modules\dicer\lib\Dicer.js:51:16)
    at emitOne (events.js:116:13)
    at HeaderParser.emit (events.js:211:7)
    at HeaderParser._finish (C:\ProjetoNode\NEAJB-MySQL\node_modules\dicer\lib\HeaderParser.js:68:8)
    at SBMH. (C:\ProjetoNode\NEAJB-MySQL\node_modules\dicer\lib\HeaderParser.js:40:12)
    at emitMany (events.js:147:13)
    at SBMH.emit (events.js:224:7)
    at SBMH._sbmh_feed (C:\ProjetoNode\NEAJB-MySQL\node_modules\streamsearch\lib\sbmh.js:159:14)
    at SBMH.push (C:\ProjetoNode\NEAJB-MySQL\node_modules\streamsearch\lib\sbmh.js:56:14)
    at HeaderParser.push (C:\ProjetoNode\NEAJB-MySQL\node_modules\dicer\lib\HeaderParser.js:46:19)
    at Dicer._oninfo (C:\ProjetoNode\NEAJB-MySQL\node_modules\dicer\lib\Dicer.js:197:25)
    at SBMH. (C:\ProjetoNode\NEAJB-MySQL\node_modules\dicer\lib\Dicer.js:127:10)
    at emitMany (events.js:147:13)
    at SBMH.emit (events.js:224:7)

  2. Good job. But, is there a way, it could allow multiple images upload at once? Also, how to read the images uploaded. Can you help.

Got Something To Say:

Your email address will not be published. Required fields are marked *

*