Vue.js + Node.js/Express RestAPIs – Mongoose ODM + MongoDB CRUD example

vue-nodejs-express-restapi-mongoose-mongodb---feature-image

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

Related Posts:
Crud RestAPIs with NodeJS/Express, MongoDB using Mongoose
Vue Router example – with Nav Bar, Dynamic Route & Nested Routes

Technologies

– Node.js/Express
– Mongoose

– Vue 2.5.17
– Vue Router 3
– Axios 0.18.0

– MongoDB

Overview

This is full-stack Architecture:

Vue-nodejs-express-restapi-mongoose-mongodb-full-stack-architecture

1. Node.js Server

vue-nodejs-express-restapi-mongoose-mongodb---nodejs-backend-mysql-architecture

2. Vue.js Client

vue-nodejs-express-restapi-mongoose-mongodb---front-vue.js

Practice

1. Node.js Backend

– Project structure:

vue-nodejs-express-restapi-mongoose-mongodb---nodejs-project-structure

Setting up Nodejs/Express project

Init package.json by cmd:

npm init

Install express, mongoose & cors:

$npm install express cors body-parser mongoose --save

-> now package.json file:

Setting up MongoDB connection

– Create a file app/config/mongodb.config.js as below content:

Create Mongoose model

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

Express RestAPIs
Route

– Define Customer’s routes in ./app/routes/customer.routes.js file:

Controller

– Implement Customer’s controller in ./app/controllers/customer.controller.js file:

Server.js

server.js file:

2. Vue Client

vue-nodejs-express-restapi-mongoose-mongodb---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-nodejs-express-restapi-mongoose-mongodb---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-nodejs-express-restapi-mongoose-mongodb---add-customers

-> MongoDB’s documents:

vue-nodejs-express-restapi-mongoose-mongodb---add-customers-select

Search Customers

vue-nodejs-express-restapi-mongoose-mongodb---search-customers

Load All Customers

vue-nodejs-express-restapi-mongoose-mongodb---load-all-customers

-> Details a Customer:

vue-nodejs-express-restapi-mongoose-mongodb---details-customers

Update Customers

– Update Katherin customer from inactive to active ->

vue-nodejs-express-restapi-mongoose-mongodb---update-customers-front

vue-nodejs-express-restapi-mongoose-mongodb---update-customers

Delete Customer

vue-nodejs-express-restapi-mongoose-mongodb---delete-customers-front

– MongoDB’s records:

vue-nodejs-express-restapi-mongoose-mongodb---delete-customers

SourceCode

Nodejs-MongoDB
Vue.js-Client

By grokonez | December 28, 2018.



Related Posts


1 thought on “Vue.js + Node.js/Express RestAPIs – Mongoose ODM + MongoDB CRUD example”

Got Something To Say:

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

*