A controller is a feature that specifies the action sent out to a customer when making a demand to the web server.
This tutorial instructs you the principle of controllers in NodeJS. We extremely advise you check out the previous tutorial Dividing Routers in NodeJS to much better recognize the Controllers.
Controllers in NodeJS
Below is the code which contains a router for the web page of an internet site.
const share = need(' share');.
const application = share();.
app.get('/', (req, res) => > {
res.send(" House");.
} );.
app.listen( 3000, () => > {
console.log(" paying attention on http://localhost:3000");.
} );.
This router utilizes the obtain() technique which takes a course and also a callback feature as a debate. The course is a path to which a customer can make an obtain demand which triggers the callback feature and also carry out the code inside it.
Callback inside the obtain technique can be made use of to specify particular performance and also send out some action in go back to the demand made by the customer so right here the callback passes inside these demand approaches called a controller
Dividing Controllers from Routers
The benefit of dividing controllers from routers is that they can make use of repeatedly in several routers by simply utilizing their names. So rather than specifying the entire callback feature in different routers we can specify a solitary controller and also utilize it inside several routers.
Below is the code where we have actually divided the controller from the router.
const share = need(' share');.
const application = share();.
const house = (req, res) => > {
res.send(" House").
};.
const regarding = (req, res) => > {
res.send(" Around").
};.
const call = (req, res) => > {
res.send(" Get in touch with").
};.
app.get('/', house);.
app.get('/ regarding', regarding);.
app.get('/ call', call);.
app.listen( 3000, () => > {
console.log(' Web server is operating on port 3000');.
} );.
Run the Code:
To run the above code, develop a job folder, and also duplicate and also paste the above code right into a JavaScript data “app.js”.
After that situate the folder in the incurable and also carry out the listed below command to set up the Express component.
After that carry out the listed below command to run the code.
Open Up the Application:
Get In the below link inside the search bar of an internet internet browser to open up the application.
Result:



Different Controllers in Private Documents
To make certain effective directing and also a clear, easy-to-debug code, internet site designers are dividing controllers right into specific data. This aids to stay clear of facility, messy code and also makes it simpler to recognize and also debug.
Tips to Different Controllers
Below is the detailed overview to divide the controllers right into specific data.
Action 1: To divide them right into specific data, develop several declare each of them.

Action 2: Replicate and also paste the controller from “app.js” to their specific data and also change their names with “module.exports” to export them in order to import them in the “app.js” data.
homecontroller.js
module.exports = (req, res) => > {
res.send(" House").
};.
aboutcontroller.js
module.exports = (req, res) => > {
res.send(" Around").
};.
contactcontroller.js
module.exports = (req, res) => > {
res.send(" Get in touch with").
};.
Action 3: Import controllers and also utilize them inside “app.js”
const share = need(' share');.
const house = need('./ controllers/homecontroller').
const regarding = need('./ controllers/aboutcontroller').
const call = need('./ controllers/contactcontroller').
const application = share();.
app.get('/', house);.
app.get('/ regarding', regarding);.
app.get('/ call', call);.
app.listen( 3000, () => > {
console.log(' Web server is operating on port 3000');.
} );.
Result:



Right here we obtain the very same outcome as in the previous instance, which implies the controller functions the very same after dividing right into specific data.
Recap
Controllers are crucial parts of NodeJS applications, as they are accountable for specifying the reactions to demands from Routers. By dividing Controllers from Routers, designers can maintain their applications clean and also efficient, which will certainly make debugging and also upkeep simpler. With a far better understanding of Controllers and also just how to utilize them, designers can develop a lot more effective and also effective NodeJS applications.
Recommendation
https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/routes