Once you have successfully installed Node.js and MongoDB latest versions on your system and init the package.json, you are ready to start your Node (Express) Server server with the MongoDB database and print the connection from the console.
In this tutorial, we are going to create a simple backend server, establish routes and the connection to the Mongo Atlas database.
Required dependences
The first step is to install the dependences necessaries for starting up the Express Server
Requirements:
• express: Node framework used for build web browser applications.
• dotenv: Loads environment variables from the .env file.
• mongoose: Create object model
Now we can start building our HTTP server.
Setup Express Server
To start the Node (Express) Server create a server.js file in a folder called “config” and require the Express dependency:
const express = require(‘express’)
const app = express() require(‘dotenv’).config()
After that, we init the middleware that parses the headers when the Conent-Type is a JSON type request.
app.use(express.json({ extended: false }))
Now choose the port where the Express server is going to stabish the http connection.
The port is a private address, don’t commit it to a remote repository; we need to separate it from the API files, creating a .env file in the root folder and writing the port, without spaces.
PORT=5000
Back in our server.js file, store the port number in a variable, using “process.env.PORT” from “dotenv” dependence, which loads the environtment variables, or use a port by default. Finish by connecting the http server and printing out the result:
const PORT = process.env.PORT || 5000
app.listen(PORT, () => {
console.log(`Server connected on port ${PORT}`)
})
To test the server, in server.js file before the port connection, create the petition to / route (localhost:5000):
app.get(‘/‘, (req, res) => res.send(‘API Running’))
And test the GET request in Postman software: The simple express server is successfully connected and running.
Setup MongoDB Atlas Database
Go to the Mongo Atlas website to connect the mongo client and create a new account. Now create a new project, I called it “node_server”; click “next” and if you want to create add members, you can invite them via email, if not, leave blank and click “Create Project”. Build a Database, select the shared database if you want the Free plan. If you want, change the settings options and the name for your database server, otherwise, leave it as it is and Create the cluster.
Create an admin user and select a strong password, don’t forget it, we will need it later. In this cluster, we will store all the fields such as “name”, “email”, etc...
Replace with the password that you have created before, and myFirstDatabase with the name of your project, in my case “node_server”.
Copy the “mongodb+srv://“ code and paste it in the .env file that we have created before.
The .env file should look like this:
Replace <password> with the password that you have created before, and myFirstDatabase with the name of your project, in my case “node_server”.
To create the connection between the server and the database we need to create a new file “db.js” into the “config” folder.
We require the mongoose package that allows MongoDB to connect to the database and create models (objects) such as employees or clients profiles.
As we did in the server.js file, we store the mongo connection address in a variable.
const mongoose = require(‘mongoose’)
require(‘dotenv’).config()
// Don't forget to require the dotenv module to read the environment variables
const mongoURI = process.env.MONGOURI
Now create a function called “connectionDB” so we can export the Mongo connection and import it later in the main server config file.
We use try and catch statement to handle all possible connection errors.
mongoose.connection.on
will print the output message “Database connected” when the connection is successful.
mongoose.connect
receive the database address (mongoURI ) and connection properties.
When an error occurs, the connection will exit and print an error message.
const connectionDB = async () => {
try {
mongoose.connection.on(‘open’, () => {
console.log(‘Database connected on:’, mongoose.connection.name)
})
await mongoose.connect(mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
})
} catch(err) {
console.error(err.message)
process.exit(1)
}
}
Export the function:
module.exports = connectionDB
To finish the Mongo setup, go to the “server.js” file and import the connection to start the node server:
const connectionDB = require(‘./db’)
connectionDB() // Call the function and connect database
Save the server.js file and run it in the terminal.
The output should be:
Server connected on port 5000
Database connected on: node_server
Create Express Routes
In the last step we are going to create routes to serve all http requests.
Request methods:
• GET: Obtain data from the http request or database information.• POST: Send information to be processed and update or create data in the server.• PUT: Update a resource in the database.• DELETE: Remove data from database.Create a new folder called “routes” in the root directory. Inside, create a file called “index.js”.
Import the Express package and create a new Router that will handle all routes.
var express = require('express');
var router = express.Router();
A middleware is an event handler that is activated under certain petitions or all of them.
We can create as many middlewares as we want.
Use the .get method to create a middleware that will receive requests at the / page (http://localhost5000/): /* GET home page. */
router.get('/', function(req, res, next) {
res.json({ msg: 'API Homepage' })
});
The { next } parameter will pass the control of the actual middleware to the specified handler, is optional.
Export the Router:
module.exports = router
In the config/server.js file, remove the line code app.get(‘/‘, (/req/, /res/) => /res/.send(‘API Running’))
that we have created before and import the routes.
Use the .use method to connect the route (/) and the imported router:
app.use(‘/‘, require(‘../routes/index’))
{
“msg”: “API Homepage”
}
You can create different routes and request methods to navigate between your server routes and add new information to the database doing a POST request to localhost:5000/create-user route and create data models with Mongoose.
Final Thoughts
From this point, you can scale your server and build the Front-End folders and connect it to your Back End server using concurrently dependency with one simple run command.
In which way are you going to continue this server?
Share post: