简体   繁体   中英

Node express routes and app.js

Ok, so I am learning nodejs and express. I am thinking that app.js is kinda like the controller, in which all my functions go. So I added the following in the index.js file under routes:

/*GET test cases */
router.get('/testcase/:id', function(req, res) {
  res.render('testcase', { title: 'Zephyr Report - Test Case', testCaseId: req.params.id  });
});

So I assume I would not right other variables and code here to pass in. So looking at other posts I am confused on how to how to write more code for this route. Also where should I put this in the app js file. Should these functions be above or below the following two lines:

app.use('/', routes);
app.use('/users', users);

Would I do something like this?

app.get('/testcase/:id', routes.testcase, function(req, res)) {
    // Code goes here
});

You probably don't want to put all of your controller logic into one file. To spread your logic around you can require another file at the top of your index.js like

tests = require('../controllers/tests_controller')

Then you can use functions that you export in the tests_controller

app.get('/tests/:id', tests.show);

You just have to export the show function in your controller

module.exports = {
   show: show
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM