I am trying to prepend a NodeJS request through express to include /api/v1. If I make an addition like to to my server.js file:
app.all('/Employees', require('./routes/Employees'));
I am able to go forward to localhost/Employees and get the proper response (it comes back from the javascript I have written in ./routes/Employees)
If I add /api/v1/ to the beginning of the app.all call, like so:
app.all('/api/*', requireAuthentication);
I am not able to go forward to localhost/api/v1/Employees. The express manual even has an explicit note about this:
Another example is white-listed "global" functionality. The example is much like before, however it only restricts paths that start with "/api":
http://expressjs.com/api.html#app.all
Any help would be greatly appreciated.
Your app gets confused whenever request is received at /api/* -- it doesn't know where to go and what to do now.
If you want to prefix /api/v1 for your requests, you can do it with couple of ways - Choose what best suits your in your case:
Mountpath way -
var express = require('express'),
app = express(),
api = express();
api.all('/employees', function(req, res){
console.log("url :: " + api.mountpath);
res.send('hit at employess');
});
//you can do this here fo v(n)
app.use('/api/v1', api);
app.listen(3000);
other way -
var express = require('express');
var app = express();
app.all('/employees', function(req, res){
res.send('/employe');
});
app.use('/api/v1', function(req, res, next){
res.redirect(req.path);
});
app.listen(3000);
Happy Helping!
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.