Reputation: 133
This is the code im using
in webserver.js
var objApiServer = require('./node_scripts/ApiServer');
app.use('/APICalls/', objApiServer);
and in ApiServer.js
module.exports = function (router, request) {
//FUNCTION
router.get('/GetDbData', function (req, res) {
/* SAMPLE CODE GOES HERE */
});
}
The on url path
//mysite.domain/APICalls/GetDbData
its working fine
but it works on
//mysite.domain/GetDbData (which i dont want)
did i missed something . what if i want to have a another function
for //mysite.domain/GetDbData
I put the another function in webserver.js with
app.get('/GetDbData' .......
but its not getting any calls
Upvotes: 0
Views: 231
Reputation: 133
I solved the issue and im posting my answer here . i had multiple routes and among them one was like
app.use('/APICalls/', objApiServer);
app.use('/RouterA', objRouterA);
app.use('/', objRouterB);
The last route was the issue. Its enables the calls to any router function without full Path. So if i call
//mysite.domain/APICalls/func1APICalls
and
//mysite.domain/func1APICalls
it will be routed to same function inside "objApiServer"
changing last line to
app.use('/B/', objClassA);
solved the issue
Upvotes: 0
Reputation: 874
Here's the demo code. Please try to implement this way:
In webserver.js --
var express = require('express');
var objApiServer = require('./router/ApiServer');
var app = express();
app.use('/APICalls/', objApiServer);
app.listen(3000);
In ApiServer.js --
var express = require('express');
var router = express.Router();
router.get('/GetDbData', function(req, res, next) {
/* SAMPLE CODE GOES HERE */
res.send("GetDbData route called");
});
module.exports = router;
Below is the screenshot of the output for above code snippet :
Upvotes: 1
Reputation: 281
Instead of passing the common router object. You may create a new router object in each routes file such as ApiServer.js, ApiServer1.js etc.
Below is th code snippet for the same:
In webserver.js
var objApiServer = require('./node_scripts/ApiServer');
var objApiServer1 = require('./node_scripts/ApiServer1');
app.use('/APICalls', objApiServer(router, request));
app.use('/GetDbData', objApiServer1(router, request));
In ApiServer.js
module.exports = function (router, request) {
//FUNCTION
return new express.Router()
.get('/GetDbData', function (req, res) {
/* SAMPLE CODE GOES HERE */
})
.post('/PostDbData', function (req, res) {
/* SAMPLE CODE GOES HERE */
});
}
In ApiServer1.js
module.exports = function (router, request) {
//FUNCTION
return new express.Router()
.put('/UpdateDbData', function (req, res) {
/* SAMPLE CODE GOES HERE */
});
}
Upvotes: 0