Express.js Integration
How to use Bootstrap 5 with Express.js - free starter
This guide will provide you with a free template for a Express.js application, with MongoDB database and Bootstrap 5 front-end.
Prerequisites
Before starting the project make sure to install the following utilities:
Creating a new Express.js application
Let's create a fresh Express application so that we can go through all the steps together. For this tutorial we'll be using MongoDB database.
Step 1
Creating Node.js API with Express and Mongoose.
Mongoose is a MongoDB object modeling tool. It helps managing MongoDB access and models. You can initialize a MDB GO starter that already has configured Mongoose.
Note: If you don't have MDB CLI installed yet, you can do it with NPM: npm install -g mdb-cli
.
Now log in with your MDB account, type: mdb login
.
If you don't have account yet you can create one using mdb register
command.
Step 2
Creating a MongoDB database.
In order to create a new database you need to run the following command.
- Create a new user
- Provide username, password, database name and description.
CLI will display your username, password, database name and connections string. You will need this data in the next step. Now you can go to phpMyAdmin where you will be able to handle the administration of the MongoDB database.
Note: the password must contain at least one uppercase letter, one lowercase letter, one number, one special symbol and have minimum length of 8.
Step 3
Open the .env
file located in the project directory and edit the DB_CONNECTION_STRING
value.
You should paste the connection string that's been shown to you after creating database.
Edited file should look like this:
Step 4
Install additional dependencies:
Step 5
Add some config to the index.js
To Do App with MDB
Step 1
We need to modify the routes/index.js
and routes/tasks.js
Step 2
Add new content to the index.html
file inside the public
directory.
Note: Remember to replace API_URL
value with your application URL.
If it's not assigned yet, run mdb backend publish -p node12
.
By default it's like https://USERNAME-PROJECT_NAME.mdbgo.io
.
For testing purposes you could use 'http://localhost:3000'
If you haven't done it already, run npm start
in your terminal.
The app should be fully functional and should work correctly with backend.
Publish your app
Now you can publish your project on MDB GO using the following command:
Note: If you don't have MDB CLI installed yet, you can do it with NPM: npm install -g mdb-cli
.
Now log in with your MDB account, type: mdb login
.
If you don't have account yet you can create one using mdb register
command.
Note Since we need to install dependencies and run your app, it may take a few moments until it will be available under the provided URL.
Express will be able to reponse for requests with data from the database using following endpoints:
-
GET /tasks
-
GET /tasks/:id
-
POST /tasks
-
PUT /tasks/:id
-
DELETE /tasks/:id
Optimization
If you want to further optimize your application please visit:
Backend features
Express.js:
This example was created with use of Express.js. By using our Express.js + MongoDB API starter we got a ready environment to start a new project.
Frontend features
MDB UI KIT:
To create the project we used our ui kit, with which we can build basic views very quickly.
Views and Layouts:
In this project we have successfully integrated the Express.js + MongoDB
API starter with the MDB Standard package.
We created simple layout using MDB components and we used those components to send
request such as get, post, put and delete
requests to the database.