How to create a Node.js API and routes?
In this Part we will create a server on our local host and API routes from scratch by explaining every line of code, dependencies and the flow. So even if its your first time with Node.js , don’t worry!
In this series we will be creating a simple Node.js API for User-Authentication firstly creating routes , handling API request then connecting it with the MongoDB Atlas and performing Database operations on MongoDB collections and using postman for testing
First, you need Node and Node Package Manager (NPM) installed on your system, you can check the version of node & npm by executing following command in your Command Line
Having installed VS code, Postman on your system and create an account on MongoDB Atlas
Step 2:Initializing Project
Create a project folder named Node Project and then we will create package.json file in that folder which is the heart of any Node.js app as it contains all the important metadata like dependencies, scripts etc. So to initialize Node.js project and create package.json file we need run following command in folder path
After the above command is executed it will ask for few inputs like , Package-name, description , entry point (index.js default), test command, git repository, keywords, author, license . If you do not give any input to it(which is acceptable) a package.json file is created like this
we need to replace “test”: “echo \“Error: no test specified\” && exit 1” by “start”:“nodemon index.js” so that whenever start our server the nodemon index.js is executed as it is our entry point. So the package.json will be. (Will talk about nodemon in Step 3)
Step 3 : Adding dependencies and creating folder structure
We need Express, Mongoose and Nodemon as our dependencies. Execute following commands to install above mentioned dependencies
npm i nodemon express mongoose
Mongoose — Connection to MongoDB database and perform database operations
Nodemon - Whenever we write our code or any changes are detected in our files, nodemon automatically restarts the app and we don't need to write npm start (initializing command to start node application) every time. We add it in package.json (Step 2)
Express - Creating single page , multipage Web-applications and creating APIs. Express is a perfect choice for a server when it comes to creating and exposing APIs . Express is something on which our entire server will be built.
2. Creating folder structure
As our main entry file is index.js so create a index.js file in our main folder then as there will be different routes so create a routes folder and in that create routes.js . For database connection , create database and a file inside it connection.js. For Schema , create a folder model and in that create a file userSchema.js , also you can create multiple schema in that model files if required. This how project folder structure will be
Step 4 : Start a localhost server
Code Code Code, here it comes. Now we will create a server using Express and run it on a localhost port. Basically it works on the request response model so lets create one in index.js. (have a look at comments)
Time to start our application. Run the following command to start our server
when we run this command the start script in package.json is executed and eventually nodemon index.js (step 2). This is how your console might look
Congratulations if you made it this far . Take a sip of your coffee. You can access your server using http://localhost:3000
Step 5 : Create routes
Now we will create Register and Login route. In routes.js add the following code .
We need to add few lines in our Index.js that is to call and use the routes module in our Index.js
Congratulations! In just 5 steps we have successfully created a localhost server and routes for our API, now in Part 2 we will connect it with the MongoDB database and design our model
Github Repository :- https://github.com/Radhaiya/Node-Project.git