Reputation: 360
Hi I'm newbie to express js i want to put multiple functions within routes, kindly explain me how to add multiple function within route, i have 2 functions in company.js but i don't know how to export it and add it in index.js
index.js
var router = require('express').Router();
var path = require('path');
// Rest API
require(path.join(__dirname, './', 'company'))(router);
// Homepage/Client
router.get('/', function(req, res, next) {
res.sendFile(path.join(__dirname, '../', 'client', 'index.html'));
});
module.exports = function(app, passport) {
// set authentication routes
require('./authentication.js')(app, passport);
// set other routes
app.use('/', router);
};
company.js
var sockets = require('../utilities/socket');
var authenticationMiddleware =
require('../middlewares/authentication.js');
var companyModel = require('../models/company.js');
var getCompanyProfile = function(router){
router.post('/api/v1/profile/fetchCompany', authenticationMiddleware.isLoggedIn,
function(req, res) {
companyModel.getCompanyProfile(req['user'].id_user,
function(result){
return res.json(result);
});
}
);
},
var saveCompanyProfile = function(router){
router.post('/api/v1/profile/saveCompany', authenticationMiddleware.isLoggedIn,
function(req, res) {
companyModel.saveCompanyProfile(req,
function(result){
return res.json(result);
});
}
);
}
module.exports = getCompanyProfile;
Upvotes: 2
Views: 2734
Reputation: 3204
You can export those two functions as follows
module.exports = {
getCompanyProfile: getCompanyProfile,
saveCompanyProfile: saveCompanyProfile
}
and import in index.js as follows
const company = require('./company.js');
company.getCompanyProfile(router);
company.saveCompanyProfile(router);
Upvotes: 3