A forum engine using Nodejs and Vuejs (part 1)

MESHILEYA Emmanuel Seun
5 min readApr 11, 2018

--

I was inspired by a colleague of mine to start writing a medium post. I got addicted to procrastinating until I finally made up my mind to put up my first medium post.

What came to my mind was to build a very simple forum (which involves user on-boarding and ability to create and comment on posts) using Nodejs on the server side and Vuejs on the client side. This project would be in series starting from the server side down to the client side.

At the end of the series, you would comfortably write apis in Nodejs using Test-Driven Development (build to make test pass) approach and consume apis with a simple forum interface in Vuejs

The series for the server side is broken down into the following outlines:

  1. Introduction
  2. Project structure walkthrough
  3. Setup mongodb with mongoose
  4. Setup crud controllers
  5. Routing

Why Nodejs ?

Nodejs is a javascript server side framework. It is asynchronous in nature which means it does not need to wait for a current task to end before it processes another one, it can always go back to wrap up its unfinished tasks.

As long as there’s something left to do, Node’s event loop will keep spinning. Whenever an event occurs, Node invokes any callbacks (event handlers) that are listening for that event.

How Nodejs application runs (source: Node.js the Right Way)

There are popular frameworks for building server side applications with nodejs. Here are a few common ones:

* Connect

* Express

* Koa

* Hapi

* Sails etc

In this project, we would be using the Express framework because its easy for creating APIs with node. Also, other frameworks are dependent on express, its pretty much easy to use and easy to test.

Here is a simple Nodejs with express application in a file named server.js

Simple Nodejs Application

If the above code looks strange, don’t worry it would be a piece of cake as we continue in the series. To run the above application, you need to have Node.js installed. Check here on how to install. Fire up your terminal or cmd, navigate to your current working directory.

Type the following in your terminal or cmd (command prompt) and hit enter

nodejs server.js

Boom!… here it comes … An error is thrown because the express module that is required is yet to be added to the project.

Error: Cannot find module 'express'

How to install module in Nodejs

One of the easiest ways to install modules in Nodejs is to use a package manager. There are two main package managers out there (npm and yarn) . I have always been a fan of npm but in this project we would be making use of yarn.

npm5 and yarn can relatively be used in place of each other.

Yeepi, I assumed you’ve installed yarn. Time to bring up express into our project.

Boom … we now have express in our project. Also, yarn automatically adds package.json and yarn.lock files into our project. Is that not awesome ?

why do we need a package.json file ?

The package.json file contains our dependencies or modules we would be using in our project. It makes it easier to install all dependencies or modules especially when collaborating on a project.

yarn install

The above command installs all the modules it could see in the package.json file. Try and delete the node_modules and run the above command. It would automatically include the express module.

package.json

{
"dependencies": {
"express": "^4.16.3"
}
}

Lets make our package.json to be more descriptive.

{
"name": "forum-engine-api",
"version": "1.0.0",
"description": "Using nodejs to write Forum engine api",
"author": "Meshileya Seun <meshileyaseun@gmail.com/>",
"dependencies": {
"express": "^4.16.3"
},
"devDependencies": {

}
}

devDependencies is like our friend that is always ready to help us with our house chores. It would contain the modules that are needed to develop our api engine e.g test dependencies while dependencies modules are needed for the engine to run e.g express.

yarn.lock

Yarn lock makes sure nothing breaks in the future when installing the dependencies. Take for example, the above express dependency is ^4.16.3. In the near future, it can be 4.16.4 and so on because the version specified allows future installation. Check here to understand more about dependencies range.

In the near future, the express module available is 5.0.0, upon installation the app stops working because of unforeseen errors.

This is where yarn lock comes in. It makes sure there is consistent module resolution.

Now lets run our first program again …

the application being fired up

Incase port 3000 is not available, feel free to change the port to any port of your choice (5000, 4000, etc).

Yeah, we have successfully run our first program in Node. Now, lets access our program running on port 3000

http://localhost:3000

Tada!!!….

Application accessed on port 3000

Lastly, we can also use yarn to run our application.

yarn start

The source code of the application can be found here.

If you have any question, kindly hit the response. I would always respond. The next part can be found here.

--

--