Setting up Node.js and MongoDB

Franklyn Ugege
BitHubPH
Published in
4 min readDec 4, 2018

What is Node.js?

Node.js came about when the developers took JavaScript that usually only ran on the browser to the server, granting them access to the low level APIs. This meant that we could create applications using JavaScript outside the context of the browser.

With Node.js we could do much more than the basic functionalities that the browser environment affords, giving us leverage to do much more like other programming languages. We would be able to do things like manipulate the file system, querying databases, creating command line applications, desktop applications (using Electronjs) and obviously creating web servers. Smiles… Feeling macho right?

Let’s take a deeper look at the formal definition of Node.js from the Node.js website:

“Node.js is a JavaScript runtime built on Chrome’s V8 engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.”

  • V8 engine is an open source JS engine written in C++ that takes JS code and compiles to machine language, this is what makes node blazing fast as computers only understand bits.
  • I/O: input/output communications from node into the Internet of Things (database, file change, http requests…).
  • Non-blocking: while one process is making a request (I/O) other processes can also be making requests. This is a core feature of Node.js and it makes applications run much faster as multiple requests can be going on at the same time.

It is worth stating that Node.js uses a single thread. All these features put together makes Node.js lightweight and efficient.

Another thing that has made Node.js so popular is the node ecosystem, its community of developers. NPM (package manager for JavaScript) is the largest ecosystem of open source libraries in the world. People develop new libraries that solve common problems, so you can focus on the specifics of your program. Node.js programmers take advantages of the vast community of developers.

How do I install Node.js?

Before we go ahead, lets install Node.js. Head to the website www.nodejs.org and download the recommended installation file with long time support (LTS).

It’s just a simple file that you can run, click next a few times and BOOM… you’re done.

To test that the installation was ok run this command node -v

node –v

It should display the version of Node.js you’ve installed.

How do I run Node JS?

Running Node.js is very simple. You basically have to run just a simple command on the CLI (command line interface).

node file-name.js

You can also access the node environment, which is very similar to the console environment on the browser by running the node command on the cli. Vouala!!!

node

Press ctrl+c twice to exit.

Install MongoDB

MongoDB, an open source document (NoSQL) database.

To download this free software, head over to https://www.mongodb.com, click on the Get MongoDB button on the top right corner of the page.

This would lead you to a download page. Click on the “Server” tab to download the MongoDB Server. For a quick Windows installer select the MSI option of the Package dropdown menu then click on the big green button to download the installer.

Go with the default installation options and you’ll be fine.

The installed MongoDB does not have a database viewer. So we would need a database visualizer to monitor the persisted data.

There are lots of MongoDB GUIs out there, so use anyone that works well for you. But we would go with Robo 3T (free lightweight GUI for MongoDB). It’s available at https://robomongo.org/. Just download and install with the default settings.

To setup the MongoDB server,

  • create a MongoDB directory, preferably the root of the harddisk.
  • Inside that directory create a “data” directory where the database files would be stored.

Next start up the mongodb server with these commands from your cmd.

cd /

cd Program Files/mongodb/server

The next folder to open is likely to differ based on the version of MongoDB downloaded. So run the directory listing command, it is most likely to be a folder labelled the version of the MongoDB you just installed (something like 3.6 or 4.0)

Next we change directory to that folder

cd x.x/bin

where x.x stands for the mongodb server version. To start up the server run the following code

mongod --dbpath= /mongodb/data

Set-up the MongoDB server and get it running with the following commands from the cli.

When you see a report “…waiting for connections on port 27017” means your MongoDB server is up and running.

Yaah!!!

We are good to start creating and running our Node.js applications with MongoDB.

--

--

Franklyn Ugege
BitHubPH

I am a fullstack developer with extensive experience in Javascript and Python and a passion for building highly configurable and user-friendly applications