This repository contains a simple API created using Node.js and Express, connected to MongoDB. If you want to visit The tutorial video I used to learn and build this API, you can find it here.
- JavaScript
- MongoDB (database)
- npm (Package manager for the JavaScript)
- Node.js (JavaScript runtime environment)
- Express.js (Node.js web application framework)
- Mongoose (Object Data Modeling (ODM) library for MongoDB)
- Nodemon (Tool for automatically restarting the node application)
- Insomnia Core (open-source API testing platform)
Before proceeding, ensure you have the following knowledge and tools:
- JavaScript knowledge
- Understanding of REST APIs
- Understanding of databases
- Knowledge of npm or a package manager and Node.js
-
Create a database in MongoDB.
-
Ensure that you are connected to the internet to successfully connect to the database.
-
Create a
.env
file and add the following line to connect to the database:MONGODB_URI=<your_mongodb_uri>
And replace
<your_mongodb_uri>
with the actual MongoDB connection URI for your specific database. Also make sure to include the necessary authentication credentials, host, port, and other relevant details in the URI. -
Run
npm run dev
to start the server. -
Visit http://localhost:3000/ to access the application.
-
Error connecting to MongoDB
: This error may indicate that you are not connected to the internet, resulting in a failed database connection. -
Error connecting to MongoDB: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist
: This error occurs when the IP address in the database is not whitelisted. To resolve this issue:-
Open your database dashboard and click on ADD CURRENT IP ADDRESS.
-
If the first step doesn't work, open whatismyip and copy your IPv4 address. In the MongoDB interface, navigate to Network Access in the sidebar menu and click ADD IP ADDRESS, then paste the copied IPv4 address.
-
Restart the server.
-
To handle the server, create a new file such as server.js
, index.js
, or app.js
, all these names are popular for entry points fo Node.js applications. In this example, app.js
will be used.
First, require the express
package and assign it to the variable express
: const express = require('express')
. Then, create an instance of the Express application using express()
: const app = express()
, This app
object represents the Express app and is used to handle HTTP requests and responses.
const express = require('express');
const app = express();
Then add a router:
-
Define a route that listens to requests
const PORT = 3000; app.listen(PORT, () => { console.log("Server Listening on PORT:", PORT); });
-
Define an endpoint Creating endpoints is done using the
app.METHOD()
:app
is our variable that creates an instance of theexpress()
andMETHOD
refers to the different HTTP methods, like GET, POST, PUT, and DELETE. It takes two arguments: the route path and a callback function that specifies what to do when a request is made to that route. Here, theapp.get()
method is used to handle HTTP GET requests.// respond with "hello world" when a GET request is made to the homepage app.get('/', (req, res) => { res.send('Hello world!') })
Here, the route path is '/' which indicates the homepage. The res.send()
function is used to send a response back to the client (in this case, the browser is the client), so when accessing the browser, it will show hello world
.
Finally, the code to set up a basic router will be:
const express = require('express')
const app = express()
const PORT = 3000;
app.get('/', (req, res) => {
res.send('Hello world!')
})
app.listen(PORT, () => {
console.log("Server Listening on PORT:", PORT);
});
Note: It is preferable to avoid changing the default variable names used in the official Express guideline, such as express
for the Express package and app
for the application object. This is recommended to maintain consistency and make the code more understandable for other developers who are familiar with the Express framework.
To run the app, use the command node app.js
(replace app.js
with your file name: node <file-name.js>
). Alternatively, you can add the command to the scripts
section in the package.json
file:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
+ "start": "node app.js"
},
Now the server can be run with the npm run serve
command.
To access the app in the browser, it will be accessible at localhost:3000
. But, ensure to run npm run serve
before accessing it.
nodemon is a tool that helps develop Node.js based applications by automatically restarting the node application when file changes in the directory are detected. nodemon does not require any additional changes to your code or method of development. (Nodemon - github)
So Nodemon is used to automatically restart the Node.js application when file changes are made, to avoid manually restarting it.
-
Install Nodemon: Run
npm i --save-dev nodemon
. -
Use Nodemon: Add a script to
package.json
and name itdev
to handle thenodemon app.js
command:"scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node app.js", + "dev": "nodemon app.js" },
Now it's possible to run the app using npm run dev
.
Pending. Details will be added later ..