How to use controllers in node and express
Hey guys what's up hoping that all is well.
Today we are going to use controllers in building our REST API in node and express.
We are going to build a server-less api and so there is no need of using a database
controllers
A controller contains functions of a particular route it controls how the requests are made to and fro the application
In the rest-api application we made here we made all our functions in onside the route files but today we are going to separate the functions in a separate file.
Pre-requisites
NodeJS
Command Line
Postman
Code editor
Step one
Navigate to one of your favorite folder using the command line and make a directory named node_controller
Then navigate to the folder created
ian@IanoTech:~$ cd Documents/REACT
ian@IanoTech:~/Documents/REACT$ mkdir node_controller
ian@IanoTech:~/Documents/REACT$ cd node_controller
ian@IanoTech:~/Documents/REACT/node_controller$
Initialize your node app
npm init -y
ian@IanoTech:~/Documents/REACT/node_controller$ npm init -y
Wrote to /home/ian/Documents/REACT/node_controller/package.json:
{
"name": "node_controller",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
This file will hold our data since we are not going to use a database
we going to use simple json data for students information
Using your favorite code editor open the folder created and then head over to data.json file
In the data.json file add
This file contains all our end points.
first install express then import it in the file as shown below then define the port as 4000.
npm i express
add the lines of codes below
const express = require('express')
const app = express()
app.use(express.json())
const port = 4000
app.listen(port, ()=> console.log(`running on port ${port}`))
Controller.js
This file contains all the functions that the app is going to use
it contains all the user defined functions and requests
Import the data.json file as shown below
const students = require('./data')
Then define our getAll function to get all our data in json format then export the functions to be used
now run the application using the command node server.js and test using postman
in the postman run localhost:4000/students to get all the data in the data.json file displayed
postman display image
Good our application is running well
Lets get to the post method
In the controller.js function add the lines below just after the getAll function
First we must define new student as an object since we are taking many entities from it
the req.body will take the input from our api then after we push the results
then use the send method for the results to be visible on the postman interface
after that we export the function just after the getAll function add newStudent function
your file should look like this
Then in our router.js file we only need one line of code
this line of code will contain our method for our newStudent function and our route
our method here is post and our route is '/new'
Test your application on postman by adding new user using localhost:4000/new end point
Use the name and admision fields only the id has already been taken care of
your final server.js file should look like
const express = require('express')
const app = express()
const studentsRoute = require('./route')
app.use(express.json())
app.use('/', studentsRoute)
const port = 4000
app.listen(port, ()=> console.log(`running on port ${port}`))
How to Overcome the 7 Most Common Mistakes of Junior Software Developers As a junior software developer, it's normal to make mistakes in the process of…
6 JavaScript frameworks you should try learning in 2023 In this blog today we are featuring the top JavaScript frameworks you should try learning in 2023 to…
How to set up react-redux : A beginners guide Introduction In this guide today, we will walk you through the process of setting up the popular library…