Hi Friends đź‘‹,
Welcome To Infinitbility ❤️!
Today, we will learn to export multiple files in a single file, for example, we have a lot of controller files in the node project but when I want to use any controller function then I don’t have to write also controller name.
Here, one more reason to do that way because in future when move one function to another controller then we haven’t worry about change imports also…
Let’s start today’s tutorial How to export multiple files in a single file in node js?
Now, we will create multiple sample controller files.
AuthController.js
1const Login = () => {2 // ...3}45const Register = () => {6 // ...7}89module.exports = {10 Login,11 Register12}
We have to create one more controller
UserController.js
1const ViewProfile = () => {2 // ...3}45const EditProfile = () => {6 // ...7}89module.exports = {10 ViewProfile,11 EditProfile12}
After that we have to create an index file behave of all controllers, here we will register all controllers.
And when we want to use any controller function we will use from the index file.
index.js
1module.exports = {2 ...require("./AuthController"),3 ...require("./UserController"),4}
Let’s see how we can use this file in the controller. Suppose, the following is our route file.
Route.js
1const { Login } = require("../Controller/index");
Here, I just provide an example to export multiple files in a single.
I Hope, You will find a solution.
All the best 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.