Express rest api get all endpoints - node.js

I have nodejs app with express framework. If I want to catch all endpoints for a http method, say 'GET', how would I define it. I tried :
app.get('*', ... )
But it doesn't seem to work. I don't want to use 'ALL' , just specific method.

As I know you should use:
app.get('/*',.......)

try using for get method
app.get('*', function(req, res) { ... });
or use this for all methods
app.get('*', function(req, res) { ... });

Related

nodejs get req.param from route

I am trying to get the url parameter of an url request from the front in my nodejs backend.
fetch(`http://localhost:9000/sent/5768797675645657`)
Here is my app.js file :
app.use('/sent/:id', require('./routes/sent'));
And my /routes/sent.js file :
router.get('/', function(req, res) {
console.log(req.params)
})
How can I get the console.log(req.params) to work?
So, app.use accepts two parameters: the portion of the URL, and a callback function.
In order to your app works, you've to change the /routes/sent.js and make it exports the function, so it can be used when you're requiring it.
Just change it to
module.exports = function (req, res) {
console.log(req.params)
}
and you're ready to go!

How to get the router values containing special characters in express router

I am working on to get route value in Nodes js using express framework
the url goes like
http://localhost:3000/course/view/turbine/turcV39/%20V42/%20V44/%20V47
Need to get the value "V39/%20V42/%20V44/%20V47" from the above url and route
router.get('/view/turbine/:turc?', function(req, res) {
console.log('a');
});
You can use regex to get it, like this
app.get(/\/view\/turbine\/turc(.*)/, function(req, res) {
console.log(req.params[0])
});

Node JS Express same thing for route GET and POST

I am working with nodejs/express. I want to do the same thing for a route, for GET and POST http requests.
I am doing this:
app.get('/', function(req, res) {
// Some code
});
app.post('/', function(req, res) {
// Same code
});
Is there a way to refactor get and post in the same callback ?
Thanks
Or you can use all, if your site doesn't use any other methods in particular:
app.all('/', handler)
This seems like a very odd requirement. If the behavior is exactly the same just specify one function to handle both:
function myHandler(req, res) {
// Some code
}
app.get('/', myHandler);
app.post('/', myHandler);
A bit old question but still facing today.
If you want to expose the api for all method with the same handler:
function sameHandler(req, res) {
// do something here
}
app.all('/', sameHandler);
If just wanna use the same route but with different handlers:
function getHandler(req, res) {
// do something here
}
function postHandler(req, res) {
// do something here
}
function deleteHandler(req, res) {
// do something here
}
app.route('/').get(getHandler).post(postHandler).delete(deleteHandler);

How do I remove a GET/POST router endpoint from express?

So I have defined an express endpoint like:
router.get('/hello', function(req, res) {
res.send('hello');
});
Later on in the code, I have something like:
router.get('/gosilent', function(req, res) {
// When this is called, it removes /hello endpoint
// such that any subsequent calls to /hello will return standard 404.
});
How do I make it work? I know how to bind endpoints but how do I unbind then?
I'm using Express 4.0.
Thanks!

Routes file issues with Passport

I'm using NodeJS, Express, and PassportJS to build a web application. I'm having a problem with one of my routes which I can't make any sense out of. When I have:
...
app.get('/auth/facebook', passport.authenticate('facebook'));
...
Everything seems to work fine. But when I change that to:
...
app.get('/auth/facebook',
function(req, res) {
passport.authenticate('facebook');
});
...
It hangs? Am I missing something on the app.get function? I want to be able to do this because I want to make the path a little more dynamic where I determine what passport authenticates. For example:
...
app.get('/auth/:provider',
function(req, res) {
passport.authenticate(req.params.provider);
});
...
Where provider could be facebook, twitter, or google...
passport.authenticate is middleware, take a gander at the source: https://github.com/jaredhanson/passport/blob/master/lib/passport/middleware/authenticate.js
passport.authenticate('facebook') returns a function that takes the req, res, next parameters from express and handles them.
so:
app.get('/auth/:provider',
function(req, res, next) {
passport.authenticate(req.params.provider)(req, res, next);
});
is what you need.

Resources