Vue.js + Nodejs/Express RestAPIs – Sequelize ORM + MySQL CRUD example

vue.js-nodejs-restapi-sequelize-mysql---feature-image

In this tutorial, we show you Vue.js Http Client & Node.js Server example that uses Sequelize ORM to do CRUD with MySQL and Vue.js as a front-end technology to make request and receive response.

Related Posts:
Sequelize ORM – Build CRUD RestAPIs with NodeJs/Express, Sequelize, MySQL
Vue Router example – with Nav Bar, Dynamic Route & Nested Routes

Technologies

– Node.js/Express
– Sequelize

– Vue 2.5.17
– Vue Router 3
– Axios 0.18.0

– MySQL

Overview

This is full-stack Architecture:

vue.js-nodejs-restapi-sequelize-mysql-full-stack-architecture.png

1. Node.js Server

vue.js-nodejs-restapi-sequelize-mysql---architecture

2. Vue.js Client

vue.js-nodejs-restapi-sequelize-mysql---vue-client-design

Practice

1. Node.js Backend

Project structure:

vue.js-nodejs-restapi-sequelize-mysql---nodejs-project-structure

Setting up Nodejs/Express project

Init package.json by cmd:

npm init

Install express, mysql, sequelize & cors:

-> now package.json file:

Setting up Sequelize MySQL connection

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

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

Create Sequelize model

./app/model/customer.model.js file:

Express RestAPIs

Route
-> Define Customer’s routes in ‘./app/route/customer.route.js’ file:

Controller :
-> Implement Customer’s controller in ./app/controller/customer.controller.js file:

Server.js

server.js file:

2. Vue Client

vue.js-nodejs-restapi-sequelize-mysql---vue-project-structure

package.json with 3 main modules: vue, vue-router, axios.
– 4 components: CustomersList, Customer, AddCustomer, SearchCustomer.
router.js defines routes, each route has a path and maps to a component.
http-common.js initializes HTTP Client with baseUrl and headers for axios HTTP methods.
vue.config.js configures port for Vue App.

For more details about how to use Vue Router in this example, please visit:
Vue Router example – with Nav Bar, Dynamic Route & Nested Routes

2.0 Setup Vue Project & Router
Init Project

Point cmd to the folder you want to save Project folder, run command:
vue create vue.js-client

You will see 2 options, choose default:

vue.js-nodejs-restapi-sequelize-mysql---vue-cli

Add Vue Router to Project

– Run command: npm install vue-router.
– Import router to src/main.js:

Define Routes

src/router.js:

App template with Navbar and router-view

src/App.vue:

2.1 Initialize HTTP Client

Install axios with command: npm install axios.
Then create http-common.js file:

2.2 Components
List of Items

components/CustomersList.vue

Item Details

components/Customer.vue

Add Item

components/AddCustomer.vue

Search Items

components/SearchCustomers.vue

2.3 Configure Port for Vue App

vue.config.js

Run

– Node.js Server: npm start.
– Vue.js Client: npm run serve.

Open Browser with Url: http://localhost:4200/.

Add Customers

vue.js-nodejs-restapi-sequelize-mysql---add-customer

-> MySQL’s records:

vue.js-nodejs-restapi-sequelize-mysql---mysql-records

Search Customers

vue.js-nodejs-restapi-sequelize-mysql---search-customer

Load All Customers

vue.js-nodejs-restapi-sequelize-mysql---all-customers

vue.js-nodejs-restapi-sequelize-mysql---katherin-customer

Update Customers

– Update Katherin customer from inactive to active ->

vue.js-nodejs-restapi-sequelize-mysql---katherin-update-customer-active-to-inactive

vue.js-nodejs-restapi-sequelize-mysql---mysql-records-update

Delete Customer

Delete Jack:
vue.js-nodejs-restapi-sequelize-mysql---delete-jack-customer

-> MySQL’s records:

vue.js-nodejs-restapi-sequelize-mysql---delete-jack-customer-mysql

Node.js Logs

-> Logs:

SourceCode

Vue.js-Client
Nodejs-RestAPIs

By grokonez | December 25, 2018.



Related Posts


Got Something To Say:

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

*