Reputation: 6240
This is my app.js:
var express = require('express');
var app = express();
var router = express.Router();
var someroute = require('./someroute')(router);
app.use('/api/someroute', someroute );
app.listen(3000, function () {
console.log('Server listening on port 3000!')
});
This is my someroute.js:
module.exports = function(router) {
var security = require('../../lib/security');
router.post("/", function(req, res, next) {
console.log('THIS IS PRINTING');
security.security_function(function(err,result){
console.log('THIS IS NOT PRINTING');
});
});
//error handler
router.use(function(err, req, res, next) {
res.status(500).json(JSON.stringify({
error: err
}));
});
return router;
}
This is my security.js file:
exports.security_function= function() {
console.log("THIS IS NOT PRINTING EITHER");
});
When I call the /api/sameroute url it hits the route because I can see the first console.log being printed, but then when I expect the security_function to print something nothing is printed. Then the result this function brings back I also want it to print, but it is obviously not printing since the security_function didn't even seem to run.
Upvotes: 0
Views: 38
Reputation: 707328
Your code is confused. You have defined security_function
to be a function that takes no arguments. It should be called simply as security.security_function()
like this:
module.exports = function(router) {
var security = require('../../lib/security');
router.post("/", function(req, res, next) {
console.log('THIS IS PRINTING');
// call the security function here
security.security_function();
});
//error handler
router.use(function(err, req, res, next) {
res.status(500).json(JSON.stringify({
error: err
}));
});
return router;
}
Then, in addition, you are not using a router properly. You are doing:
app.use('/api/someroute', someroute );
someroute
in this type of code should either be a router or it should be a middleware function. It is neither in your case. You have it as a function that does router.post()
. That's very wrong. That will register a new router.post()
handler every time the middleware is called which is never what you want to do. You don't explain what you're trying to do with this code so I can't recommend the correct code, but this code is clearly wrong.
Upvotes: 1
Reputation: 19268
you are missing the callback. please replace the security.js
file with below.
exports.security_function= function(cb) {
console.log("THIS IS NOT PRINTING EITHER");
cb();
};
Upvotes: 0