How to Connect a Node.js App to MongoDB

How to Connect a Node.js App to MongoDB
Hostman Team
Technical writer
Node.js MongoDB
21.05.2024
Reading time: 7 min

When developing Node.js applications, you might need to store data somewhere. Using application variables or files on the host machine as data storage is not always convenient. A better option to consider is connecting to an external database application.

MongoDB is great for integration with Node.js. In MongoDB, data is presented in JSON format, which works well with JavaScript. 

In this article, we'll show you how to connect a MongoDB database to your Node.js application and look at several common database queries.

This guide works for Node.js version 14 and higher and MongoDB version 4.4 and higher.

Test database

As a test database, we will use the testdb database, which contains the employees collection. It stores information about a company's employees: their department, date of birth, salary level, and other information. 

We will connect the Node.js application to this database, and we will work with the employees collection.

Creating a User in MongoDB Compass

Create a new user to work with the database. For testing purposes, we will assign the user administrator privileges for all databases; however, you shouldn't do this in production—it will negatively impact security.

Open a MongoDB Shell terminal and run the following query:

>use admin
> db.createUser({
user: "Hostman",
pwd: "password",
roles: [
{ role: "userAdmin", db: "admin" },
{ role: "dbAdminAnyDatabase", db: "admin" }
]
})

Output:

{ok: 1}

We have created a user named "Hostman" with a password "password" and will use it to connect to the database.

Setup

To connect a Node.js application to a MongoDB database, you need to install the additional mongodb package:

npm install mongodb --save
npm install mongodb-core --save

Connection

The main object through which we will interact with the MongoDB database is an object of the MongoClient class. Let's import this class:

const {MongoClient} = require('mongodb')

And declare the DBclient object using the constructor:

const MongoDBclient = new MongoClient('URI')

The class constructor is given a URI as input, which contains information about the user, IP, and server port. Here's the URI structure:

mongodb://login:password@IP:PORT/?authMechanism=method

In the case of a database hosted on a local machine, the URI looks like this:

mongodb://Hostman:[email protected]:27017/?authMechanism=DEFAULT

Here, we are using:

  • Hostman as username;

  • password as password;

  • port 27017;

  • DEFAULT as the authorization mechanism.

Let's connect to the server with the database:

const {MongoClient} = require('mongodb')
const MongoDBclient = new MongoClient('mongodb://Hostman:[email protected]:27017/?authMechanism=DEFAULT')
const connect = async() =>{
    try {
        await MongoDBclient.connect()
        console.log("Successfully connected to database")
        await MongoDBclient.close()
        console.log("Connection closed")
    } catch (e) {
        console.log(e)
    }
}
connect()

Output:

Successfully connected to database
Connection closed

Any interactions with the database are asynchronous operations, therefore, it is necessary to use async and await. Let's look at several popular operations.

Inserting documents

To insert a new document, you need to execute a query to the database with document data as an argument.

Inserting a single document

const {MongoClient} = require('mongodb')
const MongoDBclient = new MongoClient('mongodb://Hostman:[email protected]:27017/?authMechanism=DEFAULT')
const employee = {
    surname: 'Smith',
    age: 45,
    salary: 260000,
    department: 'DevRel',
    date_of_birth: '15/11/1977',
    first_name: 'John'
}
const Insert = async() =>{
    try {
        await MongoDBclient.connect()
        console.log("Successfully connected to database")
        const employees = MongoDBclient.db('testdb').collection('employees')
        await employees.insertOne(employee)
        await MongoDBclient.close()
        console.log("Connection closed")
    } catch (e) {
        console.log(e)
    }
}
Insert()

Inserting multiple documents

const {MongoClient} = require('mongodb')
const MongoDBclient = new MongoClient('mongodb://Hostman:[email protected]:27017/?authMechanism=DEFAULT')
const ManyEmployees = [{
    surname: 'Hernandez',
    age: 27,
    salary: 160000,
    department: 'Legal Department',
    date_of_birth: '15/05/1995',
    first_name: 'Juan'
},
    {
        surname: 'Miles',
        age: 30,
        salary: 200000,
        department: 'Tech Support',
        date_of_birth: '06/02/1992',
        first_name: 'Mary'
    }]
const Insert = async() =>{
    try {
        await MongoDBclient.connect()
        console.log("Successfully connected to database")
        const employees = MongoDBclient.db('testdb').collection('employees')
        await employees.insertMany(ManyEmployees)
        await MongoDBclient.close()
        console.log("Connection closed")
    } catch (e) {
        console.log(e)
    }
}
Insert()

Let's check the total number of documents in the collection after insertions:

const {MongoClient} = require('mongodb')
const MongoDBclient = new MongoClient('mongodb://Hostman:[email protected]:27017/?authMechanism=DEFAULT')
const Count = async() =>{
    try {
        await MongoDBclient.connect()
        console.log("Successfully connected to database")
        const AllDocuments = await MongoDBclient.db('testdb').collection('employees').find().toArray()
        console.log("Number of documents in the database:", AllDocuments.length)
        await MongoDBclient.close()
        console.log("Connection closed")
    } catch (e) {
        console.log(e)
    }
}
Count()

Output:

Successfully connected to database
Number of documents in the database: 5
Connection closed

Querying documents

To query documents in the database, use the following construction:

MongoClienObject.db('dbname').collection('collectionname').operation

Where:

  • MongoClienObject is an object of the MongoClient class;

  • dbname is the name of the database we are accessing;

  • collectionname is the name of the collection we are accessing;

  • operation is the query to a database or collection, for example, findOne;

If the request is made directly to the database, then collection('collectionname') is not needed.

Let's display all documents in the employees collection:

const {MongoClient} = require('mongodb')
const MongoDBclient = new MongoClient('mongodb://Hostman:[email protected]:27017/?authMechanism=DEFAULT')
const Find = async() =>{
    try {
        await MongoDBclient.connect()
        console.log("Successfully connected to database")
        const AllDocuments = await MongoDBclient.db('testdb').collection('employees').find().toArray()
        console.log(AllDocuments)
        await MongoDBclient.close()
        console.log("Connection closed")
    } catch (e) {
        console.log(e)
    }
}
Find()

Ouput:

Connection successful
[
   {
     _id: new ObjectId("637c9cbd7025c2523a76fe64"),
     surname: 'Williams',
     age: 50,
     salary: 100000,
     department: 'marketing',
     date_of_birth: '15/11/1972',
     first_name: 'Natalie'
   },
   {
     _id: new ObjectId("637ca6127025c2523a76fe65"),
     surname: 'Rubio',
     age: 35,
     salary: 200000,
     department: 'QA',
     date_of_birth: '12/06/1987',
     first_name: 'Manuel'
   }
]
Connection closed

Updating documents

Updating documents is performed in the same way as the operations above.

Updating a single document

const {MongoClient} = require('mongodb')
const MongoDBclient = new MongoClient('mongodb://Hostman:[email protected]:27017/?authMechanism=DEFAULT')
const Update = async() =>{
    try {
        await MongoDBclient.connect()
        console.log("Successfully connected to database")
        const employees = MongoDBclient.db('testdb').collection('employees')
        await employees.findOneAndUpdate({first_name: 'John'} , { $set: {first_name: "Johnny"}})
        await MongoDBclient.close()
        console.log("Connection closed")
    } catch (e) {
        console.log(e)
    }
}
Update()

Updating multiple documents

const {MongoClient} = require('mongodb')
const MongoDBclient = new MongoClient('mongodb://Hostman:[email protected]:27017/?authMechanism=DEFAULT')
const Update = async() =>{
    try {
        await MongoDBclient.connect()
        console.log("Successfully connected to database")
        const employees = MongoDBclient.db('testdb').collection('employees')
        await employees.updateMany({$or:[{department: 'DevRel'},{department: 'marketing'}]} , { $set: {department: "PR"}})
        await MongoDBclient.close()
        console.log("Connection closed")
    } catch (e) {
        console.log(e)
    }
}
Update()

Conclusion

MongoDB is a great tool, especially when coupled with Node.js. In this material, we used a local database, but there are other options, like cloud. At Hostman, you can deploy a MongoDB cloud database in a few seconds and start working in no time.

Node.js MongoDB
21.05.2024
Reading time: 7 min

Similar

Node.js

How to Update Node.js Version

Node.js stands as a robust JavaScript runtime environment powered by Chrome's V8 engine. With its capabilities, developers can construct scalable network applications with simplicity. Renowned for its event-driven, non-blocking architecture, it’s perfect for creating real-time applications. Regularly refreshing your tools ensures access to the newest features, security fixes, and performance gains. Updating Node.js regularly is crucial for the stability and security of projects, regardless of their scale. This all-inclusive guide will navigate you through diverse methods to update Node.js, covering everything from package managers to hands-on manual installations. Method 1: Via NVM Node Version Manager, abbreviated as NVM, is extensively employed for seamless handling of various Node.js versions. Its flexibility and user-friendly interface make it particularly popular among developers. This tool facilitates easy switching between node versions, perfect for projects that demand particular Node.js versions. Why Use NVM? Flexibility: Easily shift from one node version to another. Convenience: Handle installations, updates, and management of various versions effortlessly. Isolation: Isolates different versions to minimize conflicts. Step-by-Step Guide Adhere to these guidelines to set up and utilize NVM for node version management. Install NVM Initiate a terminal session and input: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash Then configure NVM in your shell profile like Bash: export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion Next, confirm the NVM installation with: nvm --version Update Version List available versions first, then proceed with installation: nvm ls-remote Subsequently, install the latest release via: nvm install node Then, set the newly installed version as the default: nvm alias default node Validate the installation through: node -v Update npm To guarantee superior performance and safety, keep npm up-to-date alongside node: npm install -g npm Lastly, validate the updated npm version for confirmation via: npm -v Switching Node Versions First, list out the installed versions through: nvm ls Next, switch to another version: nvm use <version> Insert the required version number in place of <version>. Method 2: Via Node.js Binary Packages Direct installation from Node.js official binaries is hassle-free and secures the specific release straight from the source. Why Use Binary Packages? Simplicity: Achieve direct installation without using additional tools. Consistency: Backed by the official Node.js development team. Reliability: Guarantees the exact release comes from the official source. Step-by-Step Guide Adopt these guidelines to update Node.js through binary packages. Obtain the Binary Package Access the Node.js website and grab the binary package compatible for your particular OS. Install Node.js Finish the installation by adhering to the guidelines specific to your OS: Windows: Run the installer and finish by following the on-screen steps. macOS: Execute the .pkg file and continue via the setup process. Linux: Unpack the tarball and transfer the contents to /usr/local. tar -xvf node-v<version>-linux-x64.tar.xz sudo cp -r node-v<version>-linux-x64/bin /usr/local/ Next, access the .bashrc file: nano ~/.bashrc Then, insert the lines below: export PATH="/usr/local/bin:$PATH" Save the file and use source to update settings: source ~/.bashrc Verify Installation Validate the installation via: node -v Check npm release: npm -v Method 3: Via Package Managers Homebrew and apt facilitate Node.js installation and updates by handling dependency and version management. Why Use Package Managers? Ease of Use: Simplifies installation and updates. Automation: Handles dependencies and version management. Integration: Easily integrates with other software and systems. Step-by-Step Guide Apply these procedures to upgrade Node.js via package managers. Via Homebrew (macOS) Employ the instructions on the Homebrew website if not already installed. Enter the subsequent command: brew install node For upgrading the existing installation: brew upgrade node Validate the current installation: node -v Via apt (Ubuntu/Debian) Include the node’s PPA in your system setup: curl -fsSL https://deb.nodesource.com/setup_23.x | sudo -E bash - Update 23.x to the desired release. Enter the subsequent command: sudo apt install nodejs Check that the node release is correct: node -v Method 4: Via Node.js Version Manager (n) The n package offers an efficient method for managing Node.js versions, serving as an alternative to NVM. This tool transforms the way you install, switch, and maintain different node versions on your system. Why Use n? Efficiency: Fast and lightweight. Simplicity: User-friendly and requires minimal commands. Control: Complete control over the versions set up. Step-by-Step Guide Here's how to configure and use the n package for managing node versions. Install n Run npm to globally install the n package: npm install -g n Install or Update Node.js Install the latest node release: sudo n latest Install a specific release: sudo n <version> Change <version> to the required release number. Verify Installation Confirm version: node -v Method 5: Manual Installation Manual installation suits advanced users seeking complete control over the setup. Why Use Manual Installation? Control: Complete authority over the installation workflow. Customization: Customize the build and installation settings. Step-by-Step Guide Adhere to these guidelines for manual installation: Install Dependencies Install essential dependencies first via: sudo apt install build-essential gcc g++ make Download Source Code Head over to the official website and obtain the source code. wget https://nodejs.org/download/release/v23.6.0/node-v23.6.0.tar.gz Build and Install Extract the source code: tar -xvf node-v<version>.tar.gz Replace <version> with the version number. Navigate to the extracted directory: cd node-v<version> Configure and compile the source code: ./configure make If you encounter dependency errors while running the above command, use your default package manager to install them. Install the tool: sudo make install Verify Installation Confirm version: node -v Additional Resources For in-depth information on Node.js updates and management, consider these resources: Documentation: Comprehensive resource for all Node.js things. NVM GitHub Repository: Extensive support and information for NVM. n GitHub Repository: Information on applying the n package for node version control. Release Notes: Remain informed about the newest enhancements and updates. Best Practices Regular Checks: Make it a habit to regularly check for node updates to ensure your environment is always up-to-date. Backup: Always create backups of your projects and important data before updating to safeguard against any loss during the upgrade. Testing: Upon completing the update, thoroughly test your applications to verify they function correctly with the new node release. Documentation: Keep your project documentation in sync with the latest Node.js release and note any changes from the update. Fixing Common Problems Running into problems while updating Node.js? Discover frequent issues and effective fixes: Issue 1: Version Not Changing  In case the node version stays the same after updating, attempt the following: Clear npm cache: npm cache clean -f Reinstall node using the desired method. Issue 2: Permission Errors  If permission problems occur, use sudo for elevated command execution: sudo npm install -g n Issue 3: Dependency Conflicts  Sometimes, updating the application can result in conflicts with dependencies in your current projects.  To resolve this: Utilize nvm to pick the necessary release for each project. Ensure dependencies in your project are updated to align with the new Node.js release. Conclusion There are various methods available to update Node.js, each suited to specific needs and preferences. Whether it's NVM, binary packages, package managers, the n package, or manual installation, you can keep your dev environment up-to-date with the newest features and security fixes. Frequent updates are always a key factor to maintaining top performance and safety. Consistently update Node.js to benefit from the latest features, security enhancements, and performance boosts for robust and secure applications. Using this in-depth guide, you can expertly handle and update your node installations according to your specific needs and preferences. In addition, check out our platform as a service to deploy various Node.js frameworks, such as Express, Fastify, Hapi and Nest.
23 January 2025 · 7 min to read
Node.js

How to Handle Asynchronous Tasks with Node.js and BullMQ

Handling asynchronous tasks efficiently is crucial in Node.js applications, especially when dealing with time-intensive operations like sending emails, processing images, or performing complex calculations. Without proper management, these tasks can block the event loop, leading to poor performance and a subpar user experience. This is where BullMQ comes into play. BullMQ is a powerful Node.js package that offers a reliable and scalable queuing system powered by Redis. It enables developers to transfer heavy operations to a queue in the background, keeping the main application responsive. With BullMQ you can successfully manage async queues, plan processes, and easily keep an eye on their progress. This tutorial will show you how to manage asynchronous tasks with Node.js and BullMQ. The process involves setting up a project folder, performing a time-intensive task without using BullMQ, and enhancing the application by incorporating BullMQ for running tasks in parallel. Prerequisites Before you begin, ensure you: Set up a Linux VPS server. Set up Node.js on your server. Set up Redis on your server, as BullMQ depends on Redis for managing queues. Setting Up the Project Directory Before you can use Node.js and BullMQ for asynchronous tasks, it is necessary to establish your project directory. Set up and customize your Node.js application using these guidelines. Create a New Directory Open your terminal and go to the location of your project. Create a fresh folder and navigate into it: mkdir bullmq-demo && cd bullmq-demo Initialize a New Node.js Project Set up a Node.js project using npm. It generates a package.json file containing the default configurations: npm init -y Install Required Dependencies Set up the required packages for your application: npm install express bullmq ioredis Here's what each package does: express: A fast Node.js web framework commonly used for server creation. bullmq: An excellent tool for handling queues within Node.js programs. ioredis: A Redis client for Node.js that BullMQ needs in order to establish a connection with Redis. Create the Main Application File Create an index.js file as the primary access point for your application: touch index.js Alternatively, you have the option to generate this file by using your code editor. Set Up a Basic Express Server To set up a simple Express server, include this code in your index.js file: const express = require('express'); const app = express(); const port = 3000; app.use(express.json()); app.listen(port, () => { console.log(`Server is running on port ${port}`); }); This code initiates an Express app on port 3000 which handles requests using JSON middleware. Verify the Server Setup Start the server by running: node index.js The below message should appear: Open up your internet browser and go to either http://your_server_ip:3000 or http://localhost:3000. You will receive a "Cannot GET /" message as there are no routes set up, as anticipated. When ready to proceed, you can terminate the server using Ctrl + C. Implementing a Time-Intensive Task Without BullMQ This part describes how to include a route in your Express app that performs a time-consuming task in a synchronous way. This will demonstrate how specific tasks can block the event loop and negatively affect your application's performance. Define a Time-Intensive Function Create a function in the index.js file that simulates a computationally intensive task: // Function to simulate a heavy computation function heavyComputation() { const start = Date.now(); // Run a loop for 5 seconds while (Date.now() - start < 5000) { // Perform a CPU-intensive task Math.sqrt(Math.random()); } } The function runs a loop for about five seconds, performing math operations to mimic a CPU-heavy task. Create a Route to Handle the Task Create a fresh route in your Express application that calls the heavyComputation function: app.get('/heavy-task', (req, res) => { heavyComputation(); res.send('Heavy computation finished'); }); This route is set up to receive GET requests specifically at the /heavy-task endpoint. After receiving a request, it carries out the specified intensive computation and then provides a response. Start the Server To restart your server, execute the following command: node index.js Confirm the server is functioning before moving on to the next stage. Test the Heavy Task Route Open your internet browser and type in either http://your_server_ip:3000/heavy-task or http://localhost:3000/heavy-task to access the webpage.  The following message should be displayed: It is important to observe that the response time is approximately five seconds. The delay is a result of the synchronous execution of the intensive computation process. Observe Blocking Behavior After the server is up and running, open a new tab on your internet browser and go to http://your_server_ip:3000/. The response to this request may not be immediate. The system delays taking action until the extensive processing of the previous step. This happens when the time-consuming task is blocking the Node.js event loop, stopping the server from processing additional incoming requests. When the server performs a task that takes a lot of time in a synchronous manner, it is unable to respond to additional requests. The act of blocking could result in a suboptimal user experience, particularly in apps that need to be highly responsive. Executing Time-Intensive Tasks Asynchronously with BullMQ We saw in the last section how synchronous execution of time-consuming operations can severely affect your application's performance by slowing down the event loop. This section explains how to implement a high-performance asynchronous queue into your application using BullMQ. Modify index.js to Use BullMQ Make changes to the index.js file to include BullMQ in your application. Import BullMQ and ioredis At the top of your index.js file, you should include the following import statements: const { Queue, Worker } = require('bullmq'); const Redis = require('ioredis'); Create a Redis Connection Next, set up a connection with Redis: const connection = new Redis(); Redis has been programmed to run on port 6379 and the localhost interface by default. To create a connection to a remote Redis server that has a different port, please enter the appropriate host address and port number: const connection = new Redis({ host: '127.0.0.1', port: 6379, maxRetriesPerRequest: null, }); Initialize a BullMQ Queue Create a new queue called heavyTaskQueue: const heavyTaskQueue = new Queue('heavyTaskQueue', { connection }); Add a Route to Enqueue Tasks Change the heavy-task route to add a job to the queue instead of running the task right away: app.get('/heavy-task', async (req, res) => { await heavyTaskQueue.add('heavyComputation', {}); res.send('Heavy computation job added to the queue'); }); The application will respond after a lengthy process has completed, handling requests asynchronously, when the /heavy-task route is accessed. Remove the Worker Code from index.js The worker must be implemented in a separate file. This is essential to ensure that the worker does not coexist with the Express server process. A worker's use of the heavyComputation function during execution won't interfere with the event loop of the main application. The index.js file is structured in the following way: const express = require('express'); const app = express(); const port = 3000; app.use(express.json()); const { Queue } = require('bullmq'); const Redis = require('ioredis'); const connection = new Redis({ host: '127.0.0.1', port: 6379, maxRetriesPerRequest: null, }); const heavyTaskQueue = new Queue('heavyTaskQueue', { connection }); app.get('/heavy-task', async (req, res) => { await heavyTaskQueue.add('heavyComputation', {}); res.send('Heavy computation job added to the queue'); }); app.listen(port, () => { console.log(`Server is running on port ${port}`); }); Create a Separate Worker File Generate a fresh file and name it worker.js. The file is intended for executing the worker code in charge of handling tasks obtained from the queue. Create the worker.js file: touch worker.js Add Worker Code to worker.js: const { Worker } = require('bullmq'); const Redis = require('ioredis'); const connection = new Redis({ host: '127.0.0.1', port: 6379, maxRetriesPerRequest: null, }); // Function to simulate a heavy computation function heavyComputation() { const start = Date.now(); // Run a loop for 5 seconds while (Date.now() - start < 5000) { // Perform a CPU-intensive task Math.sqrt(Math.random()); } } const worker = new Worker( 'heavyTaskQueue', async job => { // Time-intensive task here heavyComputation(); console.log('Heavy computation completed'); }, { connection } ); worker.on('completed', job => { console.log(`Job ${job.id} has completed`); }); worker.on('failed', (job, err) => { console.log(`Job ${job.id} has failed with error ${err.message}`); }); Run the Worker in a Separate Process You must now execute worker.js as an independent Node.js process. Start the Worker Process Open a new terminal window or tab, navigate to your project folder, and run the specified command: node worker.js Start the Express Server Initiate the Express server in your original terminal window: node index.js Test the Application with BullMQ Proceed to conduct testing of the application utilizing BullMQ.  Make a Request to /heavy-task:Open your internet browser and type in either http://your_server_ip:3000/heavy-task or http://localhost:3000/heavy-task in the URL bar. The following message should be displayed: Heavy computation job added to the queue. The rapid response time suggests that there is no blockage in the main thread. Adding a Dashboard to Monitor BullMQ Queues Monitoring your application's queues and jobs is essential for ensuring they are functioning properly and for troubleshooting purposes. BullMQ comes with a functionality called Bull Board, which offers a visual interface for overseeing your queues. This part explains how to incorporate a dashboard into your application. Install Bull Board Use npm to install the @bull-board/express package: npm install @bull-board/express Set Up Bull Board in Your Application In order to set up the bull board application, follow these steps: Import Bull Board Modules Insert the code provided at the top of your index.js file: const { createBullBoard } = require('@bull-board/api'); const { BullMQAdapter } = require('@bull-board/api/bullMQAdapter'); const { ExpressAdapter } = require('@bull-board/express'); Create an Express Adapter for the Dashboard Initialize the Express adapter: const serverAdapter = new ExpressAdapter(); serverAdapter.setBasePath('/admin/queues'); Set Up Bull Board with Your Queue Create the Bull Board instance and pass your queue: createBullBoard({ queues: [new BullMQAdapter(heavyTaskQueue)], serverAdapter: serverAdapter, }); Use the Dashboard in Your Express App Add the following line to mount the dashboard at /admin/queues: app.use('/admin/queues', serverAdapter.getRouter()); Make sure to include this line following the setup of your queue and worker. The final index.js file looks like below: // Import Express and Initialize App const express = require('express'); const app = express(); const port = 3000; app.use(express.json()); // Import BullMQ and Redis const { Queue } = require('bullmq'); const Redis = require('ioredis'); // Redis Connection const connection = new Redis({ host: '127.0.0.1', port: 6379, maxRetriesPerRequest: null, }); // Initialize Queue const heavyTaskQueue = new Queue('heavyTaskQueue', { connection }); // Define Route to Add Job to Queue app.get('/heavy-task', async (req, res) => { await heavyTaskQueue.add('heavyComputation', {}); res.send('Heavy computation job added to the queue'); }); // Import Bull Board and Set Up Dashboard const { createBullBoard } = require('@bull-board/api'); const { BullMQAdapter } = require('@bull-board/api/bullMQAdapter'); const { ExpressAdapter } = require('@bull-board/express'); const serverAdapter = new ExpressAdapter(); serverAdapter.setBasePath('/admin/queues'); createBullBoard({ queues: [new BullMQAdapter(heavyTaskQueue)], serverAdapter: serverAdapter, }); app.use('/admin/queues', serverAdapter.getRouter()); // Start the Server app.listen(port, () => { console.log(`Server is running on port ${port}`); }); Access the Dashboard To access the dashboard, follow the steps listed below: Restart Your Server node index.js Navigate to the Dashboard Open your browser and go to http://your_server_ip:3000/admin/queues. Explore the Dashboard: Queue Overview: See the list of queues and their status. Jobs List: View active, completed, failed, and delayed jobs. Job Details: Click on a job to see its data, logs, and stack trace if it failed. You can easily manage your BullMQ queues by integrating Bull Board into your application. It is much easier to keep an eye on progress and identify issues when you can view your queues and tasks on the dashboard in real-time. Conclusion You have now learned how to use BullMQ with Node.js to manage asynchronous processes. Your application's responsiveness and efficiency have been enhanced by moving time-consuming operations to a separate queue. Your Node.js app is now much more capable of handling heavy demands thanks to the usage of queues.
28 November 2024 · 11 min to read
Node.js

How to Install Node.js and NPM on Ubuntu 24.04

The popular JavaScript runtime Node.js enables server-side programming with JavaScript. NPM, a package manager for Node.js projects, helps with dependency management. This guide will show how to install NPM and Node.js on Ubuntu 24.04. Prerequisites System (or a cloud server) running in Ubuntu 24.04 Root access or user with sudo privileges Installing Node.js and npm from the Default Ubuntu Repository Update the package lists to ensure to have the most recent information on package versions and dependencies. Run the command below:  sudo apt update && sudo apt upgrade -y Node.js is normally available from Ubuntu's default repository. Install it by running the following command: sudo apt install nodejs npm  -y Installing Node.js and npm via the NodeSource Repository Add the NodeSource repository for Node.js:  curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -  Replace setup_20.x with the desired version. Different version can be found on nodesource.com. Use the following command to install Node.js after adding the NodeSource repository: sudo apt install nodejs -y Verifying the Node.js and npm Installation Verify the following versions of Node.js and npm to make sure they were installed correctly. Run the below command. node -v npm version Installing Specific Node.js Versions with NVM  With the help of the robust utility Node Version Manager (NVM), devops may easily manage several Node.js versions on a single machine. This is very helpful when switching between several project needs. To install NVM, download and run the installation script from the NVM repository using the following command: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash After running the scripts, source the user profile and add NVM to the shell session. Add the following lines to the user's home directory (~/.bashrc, ~/.zshrc, or the corresponding shell profile script). Create it using nano editor: nano ~/.bashrc 3. Add the following content: export NVM_DIR="$HOME/.nvm"[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" Run the command below so changes will take effect. source ~/.bashrc With NVM installed, install the specific versions of Node.js. In this case, to install Node.js version 16, run the command below: nvm install 16 Switch to a specific version of Node.js that is installed, using the command below. nvm use 16 Managing Node.js Projects Several essential procedures and best practices are involved in managing Node.js projects in order to ensure the effectiveness, maintainability, and scalability of the application. This is a tutorial to help to efficiently manage your Node.js projects. Launch the terminal, navigate to the project creation path, and make a folder named after the project you are creating. mkdir my_project Initiate the Node project by running the command npm init. Provide the required details (marked in red). All of the answers in this example will be default. The file package.json will result from this. npm init Install any required dependencies, such as nodemon and express. The package-lock.json file and the node_modules folder will be created as a result. npm i express nodemon To initialize git in the project, use the git init command. This will include the file .gitignore. git init Make a file called Readme.md that will have all of the project's information. touch Readme.md Make a file with the .env extension that will hold the project's credentials and sensitive data. touch process.env To launch the program, create a file with the name app.js or index.js. touch app.js Make two folders: Public (which contains resources and static files) and src (which contains controllers, models, routes, and views). mkdir Public src Check each and every folder and file that was generated. This is how a typical structure might look like. For the NODE JS application, it is best practice to establish a project structure, divide files based on their function, and place those files in the appropriate directories. To make it simple to confirm the existence and logic of any given file or folder, unify the application's naming conventions and include business logic in the controllers folder, for instance. ls -lrt Best Practices for Node JS Project Structure For production systems, set up logging and monitoring with tools like Datadog or New Relic. Plan routine maintenance activities including performance reviews, security audits, and dependency updates. Put in place a backup plan for important configurations and data. Check for security flaws in your dependencies and code on a regular basis. Troubleshooting Common Issues There are some frequent problems that a user could run into when installing npm and Node.js. These troubleshooting instructions should help you to address the majority of typical problems that arise when installing npm and Node.js. The steps for troubleshooting these issues are listed below: When attempting to install Node.js or npm globally (i.e., using sudo), users get permission-related issues that prevent them from finishing the installation process. After installing nvm, the command is not recognized. The error nvm Command Not Found will be encountered. Make sure that the shell's configuration file (.bashrc, .bash_profile, .zshrc, etc.) has nvm sourced, and then the command source ~/.bashrc has been use to reload it. The npm version is out of date or does not correspond with the Node.js version after installing Node.js. Use nvm install <version> to install a particular Node.js version, which will include the matching npm version, and manually update npm by running npm install -g npm.  Conclusion In conclusion, an important initial step in creating new web applications and utilizing server-side JavaScript is installing Node.js and npm. Although installing software is usually simple, there are a few frequent problems that can arise, such as permissions conflicts, environment setup problems, or version mismatches. One can successfully overcome these problems by configuring npm to be compatible with your network environment, modifying system settings for global installations, and managing Node.js versions with tools like nvm. Do not forget to update npm and Node.js frequently to take advantage of the newest features and security updates. It will have a strong base for developing and implementing Node.js-powered, scalable applications with correct setup and troubleshooting.
09 September 2024 · 6 min to read

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support