DIIMIIM
DIIMIIM

Reputation: 599

How to app.use() a file that has some other app.use() within nodeJS

So maybe it sounds pretty tricky at a first look.I'll explain what I actually want to do : I'm working with NodeJS & expressJS and I'm trying to modularize the project,here is what I want to do: Let's say I have the in the main server .js file the router.get / post / put / delete etc splitted in some other files for every type : Ex :

...
app.use(require('./routes/account_method1.js'));
app.use(require('./routes/account_method2.js'));
app.use(require('./routes/games_method1.js'));
app.use(require('./routes/games_method3.js'));
app.use(require('./routes/emails_method5.js'));
...

I performed that pretty easy : I just used the code mentioned above in the main .js file and in the required files I've just put :

module.exports = router;

And that's pretty much it.But the problem is : Now I want do to something like :

...
app.use(require('./routes/account/panel.js'));
app.use(require('./routes/games/panel.js'));
app.use(require('./routes/emails/panel.js'));
...

And in every ./routes/x/panel.js file to have the specific .js files required withing , for just an example in ./routes/account.panel.js I would like to have and work :

app.use(require('./account_method1.js'));
app.use(require('./account_method2.js'));

Assuming account_method1.js and account_method2.js are in the same directory with the specific panel.js file.And then,require these panel.js files in the main server .js file.

So if now I have :

 Server ---> ./routes/account_method1.js
        ---> ./routes/account_method2.js

I would want to make it :

 Server ---> ./routes/account/panel.js ---> ./routes/account_method1.js
                                            ./routes/account_method2.js

I won't paste here any code because it's just a problem about a way of importing files and I've already mentioned how I require my .js files on the main server.

Soo...what do you think?

EDIT :

I think will show you where is the problem :

./routes/test.js ( this file have to add to app another file,register.js)

 const express = require('express');
 const router = express.Router();

 console.log("BEFORE ROUTER.USE");
 router.use(require('./register.js'));
 console.log("AFTER ROUTER.USE");

module.exports = router;

./app.js (the main .js server file)

 ....
 var test = require('./routes/test.js');
 app.use(test);
 ....

./routes/register.js

 const express = require('express');
 const router = express.Router();
 ...
 //some router.post / get / delete 
 ...
 module.exports = router;

And when I try to access the URLs from that register.js file they aren't working

Upvotes: 0

Views: 1813

Answers (1)

bato3
bato3

Reputation: 2815

Better way is using router:

eg: controller/api.js

const express = require('express')
const router = express.Router()

  router.get('/', function(req, res) {
      //console.log(res.apiData);
      res.json({message:'ok'});
  });

  module.exports = router;

and later

app.use('/api', require('controller/api'))

Router can be imported to another router. I'm use this:

main.js:

app.use(require('./controller'));

controller/index.js:

const express = require('express')
const router = express.Router()
const isAuth = require('../middlewares/is-auth')
const isValidApiKey = require('../middlewares/is-api-key')
const isAdmin = require('../middlewares/is-admin')

router.use('/auth', require('./auth'));
router.use('/profile', isAuth, require('./profile'));
router.use('/api', isValidApiKey, require('./api'));
router.use('/admin', isAuth, isAdmin, require('./admin'))

If the request service code is long, you can also move it to a separate file, but I think it's better to divide it into smaller, reusable parts and use it as middleware, eg:

route.all('/:orderId(\\d+)', fileOrderMiddleware,  fileAltMiddleware, hasTransferMiddleware, orderServeMiddleware);

Upvotes: 1

Related Questions