I have started on the journey of developing a project in my spare time (it’s all hush hush for now ;) haha), this will involve me creating an API using NodeJS, React-Native app as well as an Electron desktop application, so I thought it would be cool to document various points of this project that I find interesting, so this article will be regarding the first step of me developing my API that will be used and future articles will explain how I linked this with MongoDB and Heroku.
So todays article will be about how to get started creating an API using NodeJS.
The first step is to create a folder your project in a suitable area on you Macbook/PC, then open your preferred code editor (Mine is visual studio code) and open the folder you just created within the editor. Secondly, now you need to navigate to this folder in your terminal within the code editor using “cd enterProjectFolderNameHere”.
The next step is to enter the command “npm init” this will create a new npm project/package (Including a package.json file) and ask you to enter specific information for each header that comes up. This information will then be present in your newly created package.json file. Some of the headers found within the package.json file include “name”, “version”, “main”, “scripts” and author.
After this has finished you should a “package.json” file on the left hand side.
The next step is to create a file that will contain your API end points, in the terminal of your code editor, type in the command
This folder will contain the files (That we will be creating in future articles) necessary for your MongoDB schema that we will create a database for using MLab and use the package “Mongoose” to communicate with our database from the API.
Ok so now we need to install some very important packages, one being express, the second being “body-parser” and the third being “mongoose”, enter the command
npm install mongoose express body-parser
This will save you from entering an npm install command for each package.
After installing all of these you can check in the “node_modules” folder to ensure they are present.
Writing code time!
Write so now we get to the fun stuff, the first thing you will need to do is require the “express” package so we can use it in “server.js”.
var express = require('express');
We will then need to make an instance of this with the below code
var app = express();
From the “app” variable we can now access all of the functions available within the “express” package.
We will also need to create a variable for the port that we will be using. (We don’t need to make a variable but it is good practice to do so)
var port = process.env.PORT || 3000;
NOTE: the code “process.env.PORT || 3000” is used because you will usually upload your API to a hosting service such as Heroku and they may configure the “process.env.PORT” variable themselves since your code will be running in their environment after you upload it to their services so your code will use the environment variable of the system it is hosted or otherwise use port 3000.
The next step is to require the next 2 packages that we installed as seen in the code below.
var mongoose = require(‘mongoose’);var bodyParser = require(‘body-parser’);
Mongoose: Mongoose is an ODM (and no I don’t mean Old Dirty Man) I mean an Object Data Modelling tool, and in English that basically means that Mongoose handles the relationships, schemas and object translations between the Mongoose package and the MongoDB database, objects in MongoDB are represented as JSON objects.
body-parser:body-parser is an incredibly important package as it allows us to “parse” the data we are sending to the database to a specific format such as “json” therefore creating a middleware between our software and the end of the request as body-parser knows how to correctly read the body of the request.
So at the moment our “server.js” file is looking very bare, but I promise in the next tutorial/article it will be looking a lot larger and less hungry, I will most likely have part 2 published tonight or tomorrow, but the UK weather at the moment is almost unbearable and I am about to eat a very delicious Ice cream and milkshake so I will be busy with that for a while.
As always, any criticisms, feedback or general comments are appreciated.
Stay hydrated UK!