How to Install MongoDB, Express, Angular, and Node.js on an Ubuntu Cloud Server?

31-01-2024 02:39:16

MEAN is an open-source JavaScript framework for developing both front-end and back-end web applications. This framework allows for the creation of website frontend and backend programs using only JavaScript. MEAN stands for MongoDB, Express, Angular, and Node.js. These are independent technologies that, when integrated, provide enhanced functionality. First, we need to prepare a cloud server with Ubuntu 20.04 operating system installed and update it with the latest patches.

Installing MongoDB

Execute the following commands to install MongoDB and start MongoDB.

$ sudo apt install -y mongodb
$ sudo systemctl start mongodb
$ sudo systemctl enable mongodb

Installing Node.js and Its Dependencies

Use the official NodeSource repository to install the latest version of Node.js.

$ curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
$ sudo apt install -y nodejs gcc g++ make git

Install yarn and gulp using npm.

$ sudo npm install -g yarn
$ sudo npm install -g gulp

Clone the official MeanJS repository and use yarn to install the dependencies.

$ git clone https://github.com/meanjs/mean
$ cd mean
$ yarn install

Coding

Edit the server.js file.

$ nano server.js

Add the following code to the server.js file.

const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const app = express();

app.use('/', (req, res) => {
    MongoClient.connect("mongodb://localhost:27017/test", function(err, db){
        db.collection('Example', function(err, collection){
            collection.insert({ pageHits: 'pageHits' });
            db.collection('Example').count(function(err, count){
                if(err) throw err;
                res.status(200).send('Page Hits: ' + Math.floor(count/2));
            });
        });
    });
});

app.listen(3000);
console.log('Server running at http://localhost:3000/');

module.exports = app;

Testing

Finally, compile using the gulp tool.

$ gulp

To verify that the server is running correctly and the database is connecting properly, visit: http://192.168.0.123:3000. If the message "Page Hits:" appears, it indicates that the MEAN platform has been successfully installed. Replace 192.168.0.123 with the actual IP address."