Introduction.
Hello, there. It's been a while. I hope you are well. This article is about what a project did a while ago as an end-of-the-semester project in the school I am currently learning Software Engineering. This school is AltSchool Africa and I recommend it to anyone who wants to start a career in Software Engineering.
This project is about building a Blog API. I implemented this project with NodeJs, ExpressJs, and MongoDB and I will be talking about how I implemented this project in a series of articles. Also, I will discuss some additional features I didn't add initially when I did the project towards the end of the series. You should be familiar with NodeJS, ExpressJS, MongoDB, Git, and npm. So let's get started.
Project Setup Guide.
Here we will be creating the project folder with git and running a simple server with the following instructions.
Open git from your computer. If you don't have git installed, here is a link on how to go about that. You will also need to configure git.
On your desktop git app, you can go ahead and create a Projects folder by using this command
mkdir Projects
. This will create a folder on your computer. To open the folder, use this commandcd Projects
.Now you are in the Projects folder, create a folder with the name of your Project. I named mine "Talk-Your-Mind" thus I will run this code
mkdir Talk-Your-Mind
.Next is to enter the directory with this command:
cd Talk-Your-Mind
.Create a file called app.js with this command:
cat > app.js
. This will prompt you to input it into the file. You can addconsole.log("Hello World")
and press ctrl+c to terminate the process.Type
code .
on the prompt and press Enter key. This will open up your default IDE and you are ready to code.
Creating a Simple Server.
When you are trying to build a backend application, you will need to install third-party modules called Packages. To install these packages, you need to first initialize your current working directory. To do this, run the npm init
command on the vscode terminal and follow the prompts as they come.
After executing all the prompts, you will automatically see the package.json file. This file keeps records of all the important metadata of the project as well as defines some attributes that npm uses to install dependencies.
Since we will be using ExpressJS, you will need to install it. To install a package, you need to execute this command: npm install package_name. In the case of express, it going to be npm install express
. Also, install the dotenv package. Before we finally start writing code, create two(2) new files called "index.js" and ".env" files.
const express = require("express");
const app = express();
app.use(express.json());
module.exports = app;
The code above goes into the app.js file we created earlier before. This code above simply imports the express package we install and we called the express function and save that into a variable called app. Then we go further to export the app.js file.
const app = require("./app");
require("dotenv").config();
const PORT = process.env.PORT;
app.listen(PORT, () => {
console.log(`App is listening from PORT: ${PORT}`);
});
The code above goes into the index.js file. The app.js we exported is now imported here. In the .env file, declare a variable PORT and assign 3000 to it.
Now back to our index.js file, we require dotenv
and configure it so that process.env
now has the keys and values you declared in your .env file. app.listen
is used to bind and listen to connections on a specific port and hostname. It accepts the port and a callback function.
Give it a save and execute node index.js on the terminal to run the server. At this point, you should see "App is listening from PORT: 3000". HURRAY!!!, the server is running.
Conclusion.
In this article, we discussed the project, setting it up and how to create a server running on our computer. The next article will discuss how to connect our app to a database. See you there.
Thanks for reading to the end.