How To Deploy A NodeJs/Express Application On Cyclic.sh With A MongoDB Database
Published: 2023-11-16 02:24:42

Deploying the backend application of any project and whatever programming language or framework isn't a walk in the park.

The reason for this is, that when deploying a backend application or pushing it into production, a lot of factors are usually considered.

Some of these are; scalability, flexibility, ease of configuration, and a lot of others.

And with some of these factors, the deployment process gets unnecessarily complicated.

However, with Cyclic.sh, deploying your Nodejs/Express application is a simple process.
In this tutorial, I will show you how to deploy a Nodejs/Express application using Cyclic.sh, using MongoDB as your database.
However, before that, let's do a quick rundown on each of the concepts or technologies we will be referring to.

## What Is Nodejs?
As you might have already known, NodeJs is an open-source Javascript run time environment, that allows you to run Javascript code outside of your browser.
It allows developers to use Javascript to write server-side code.
It makes use of the Chrome V8 engine, of which V8 compiles Javascript directly to native machine code.

## What Is ExpressJs?
According to the ExpressJs website, _Express is a minimalistic and flexible node js web application framework, providing a robust set of features for building single and multiple and hybrid web applications_.

What this means is that ExpressJS simplifies the process of building robust web applications by providing key features and tools for the creation of web applications.

It's a popular choice amongst NodeJS developers because it is lightweight, designed for flexibility, and opinionated.

## What Is MongoDB?
MongoDB is an open-source NoSQL or non-relational database management system that makes use of the document-oriented data model.

Of course, it has features such as indexing, schema-less (non-rigid schema), document-oriented, and a lot of other features, which make it different from other non-relational databases.

## What Is Mongoose?
Mongoose is a popular ODM (Object Data Modeling) library that makes it easy to work with Node Js/Express and MongoDB.

It provides a schema-based solution for modeling the data of your application.

## How To Create A Cyclic.sh Account 
Creating a Cyclic.sh account is an easy task, and all that is required is a GitHub account.
Here are steps on how to create a Cyclic.sh account - 

- 
Visit the Cyclic.sh website 

- 
Tap on Sign Up 

- 
Tap on Continue with GitHub

If done properly, your Github account will be automatically linked to your Cyclic.sh account.

## How To Deploy A NodeJs/Express Application On Cyclic With A MongoDB Database 
Having signed up on Cyclic.sh, your Github repository automatically gets linked to the Cyclic account, and you can now deploy your Node Js applications, including the private repositories, provided that you provide their link.
As I had earlier revealed, deploying a Nodejs/Express application with a MongoDB database is slightly different from most of the other databases.
Here are steps on how to deploy them, depending on your method: 

##1. Using MongoClient - 

```
const { MongoClient } = require('mongodb');
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000

const uri = process.env.MONGO_CONNECTION_STRING;
const client = new MongoClient(uri);

//Routes go here
app.all('*', (req, res) => {
    res.json({success:true})
})
 


client.connect(err => {
    if(err){ console.error(err); return false;}
    // connection to mongo is successful, listen for requests
    app.listen(PORT, () => {
        console.log("listening for requests");
    })
});
```

## 2. Using Mongoose - 

```
const express = require('express')
const mongoose = require('mongoose')

const app = express()
const PORT = process.env.PORT || 8080

const connectDB = async () => {
  try {
    const conn = await mongoose.connect(process.env.MONGO_URI);
    console.log(`MongoDB Connected Successfully);
  } catch (error) {
    console.log(error);
    process.exit(1);
  }
}

//Routes go here
app.all('*', (req, res) => {
    res.json({success:true})
})

//Connect to the database before listening
connectDB().then(() => 
    app.listen(PORT, () => {
        console.log(`Server started on Port ${PORT}");
    })
})

```
The reason why Express applications with a MongoDB database are deployed this way is because MongoDB is not an on-demand database, and it usually takes a while for it to establish a connection with the server.
So, with this method, we get to run it asynchronously by wrapping our app.listen in a .then.catch, or with an async…await, which ensures that the database is carried along when the server is listening to connections.