Node.js/Express – PostgreSQL example – Upload File/Download File – Multer + Sequelize CRUD

In the tutorial, we show how to build a Node.js/Express RestAPIs to Upload Files/Download Files to PostgreSQL using Multer middleware and Sequelize ORM.

Related posts:
Multer – Build RestAPI to upload a MultipartFile to NodeJS/Express
NodeJs/Express RestAPIs – POST/GET/PUT/DELETE requests

Technologies

  • Node.js
  • Express
  • Multer
  • Sequelize
  • PostgreSQL

Goal

We create Node.js project as below:

nodejs-express-restapis-multer-upload-files-download-files-sequelize-postgresql + nodejs-project

Expose 3 RestAPIs:

  • router.post('/api/file/upload', upload.single("file"), fileWorker.uploadFile);
  • router.get('/api/file/info', fileWorker.listAllFiles);
  • router.get('/api/file/:id', fileWorker.downloadFile);

Upload file:

nodejs-express-restapis-multer-upload-files-download-files-sequelize-postgresql + post-request-upload-file

-> PostgreSQL’s records:

nodejs-express-restapis-multer-upload-files-download-files-sequelize-postgresql + postgrsql-records

Download file:

nodejs-express-restapis-multer-upload-files-download-files-sequelize-postgresql + get-file

Practice

Setting up NodeJs/Express project

Create a folder Node.js-Express-Upload-Download-Files-PostgreSQL:

Then init Node.js project:

Install Express, Multer, Sequelize, PostgreSQL:

– Multer is a node.js middleware for handling multipart/form-data , which is primarily used for uploading files.
– Sequelize is a promise-based ORM for Node.js v4 and up. It supports the dialects PostgreSQL, MySQL …

-> package.json file:

Sequelize Model

– Create a File model, see file ./app/models/file.model.js:

Multer Config

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

Setting up PostgreSQL connection

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

– Setup PostgreSQL connection with Sequelize in ./app/config/db.config.js file:

Express Routers

– Create ./app/routers/file.router.js file:

Download/Upload Controllers

– Create ./app/controllers/file.controller.js file:

Server.js

– Create ./server.js file:

Run & Check results

Start Node.js server -> Logs:

-> Upload Files, Logs:

nodejs-express-restapis-multer-upload-files-download-files-sequelize-postgresql + post-request-upload-file

-> Get File’s info, Logs:

nodejs-express-restapis-multer-upload-files-download-files-sequelize-postgresql + get-request-file-info

-> Download Files, Logs:

nodejs-express-restapis-multer-upload-files-download-files-sequelize-postgresql + get-file

SourceCode

Node.js-Express-Upload-Download-Files-PostgreSQL

By grokonez | July 12, 2018.



Related Posts


Got Something To Say:

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

*