Test nodejs using postman: How to get req.user in postman - node.js

I'm using passport for register & login function. My API is like this:
router.post('/secret', ctr);
(req, res, next) => {
if (req.user) {
...
}
}
How to test this API using postman?

Your question is not clear. First refer to the postman documentation.
If what you are trying to achieve is to send in the "user" field properly then in Postman set the method to POST, and Content-Type to application/json and send in a valid JSON body with a "user" field in it with your request.
Your Node app should have body-parser installed as well.

Related

Access URL query Params in an Express POST route

I have a NodeJS/Express application.
From an url endpoint titled: localhost:3000/form?someProp=someValue&somethingElse=someOtherValue
This page submits a form to another Express endpoint.
I submit to an Express POST endpoint. I know in a GET endpoint I could access the query params via req.query, but I cannot seem to do that in a POST request.
Is there a way to access the query params from the request in a POST route?
(other than splicing the header.referrer... which I may just have to do)
Here are some code snippets:
If I submit to the first route it works, if I submit to the second... it does not.
router.get('/test',
(req, res) => {
console.log(req.query); // {someProp: someValue, somethingElse: someOtherValue }
}
);
router.post('/test2',
(req, res) => {
console.log(req.query); // returns {}
}
);
So I tried to send a simple request to test it and got everything working without doing anything special (The only thing extra I have is that I'm using body-parser middleware from npm):
app.use(bodyParser.json({limit: '50mb'}));
Then I tried this simple route and got the result query params as you can see in the picture attached.
http://localhost:8080/test?test=1&what=2
Any chance you're sending the other form request from the client in a different way? try looking at the network in chrome and see if you sending what you expecting. Obviously, there is something missing here as it worked for me without doing anything special.

How to get req.body from Facebook's data deletion URL call?

I've implemented the "sign in with Facebook" authorization method for my Express.js Node app. Since the app is hosted in the EU, Facebook requires it to have a data deletion request callback URL.
I've created an endpoint for the data deletion request, but whenever I make Facebook call that endpoint both req.body and req.query are empty - if I've understood correctly the body should contain a signed_request that could be used to e.g. verify the caller.
My CORS settings should be fine and I've tested my endpoint by calling it from Postman. The endpoint is able to receive a POST request with a JSON body without any problem.
So, what am I doing wrong - why does it seem like Facebook is calling my endpoint with a POST request that has an empty body?
My endpoint:
import express from 'express'; // 4.17.1
const router = express.Router();
router.post('/fb_data_deletion', (req, res, next) => {
console.log(req.body); // {}
console.log(req.query); // {}
if (!req.body || !req.body.signed_request) {
console.log('Bad request'); // Ends up here whenever Facebook calls this route
return req.sendStatus(400);
}
// verify request, delete user's data + other code here
});
Turns out Facebook isn't sending a POST request that uses Content-Type application/json but application/x-www-form-urlencoded.
To get the body of Facebook's POST request I had to add the following line to my app.js where the Node server is being set up:
app.use(express.urlencoded());

Nodejs doesnt receive jsondata

My Node.js server is not able to send the data it receives back to the browser. It is able to send self-made data like this
app.post('/login', (req,res) =>{
console.log(req.body);
res.setHeader('Content-Type', 'application/json');
res.json({test: "test info",test2: "still testing"});//works
//res.json(req.body);//does not work
})
In the fetch POST request, I have set the header 'Content-Type': 'application/json'.
In the firefox debugger, I can see that the information is sent via POST.
I have tried stuff like
res.json(req.body);
res.json(req.body.pass);
res.json({pass:+req.body.pass});//response in browser -> {"pass":null}
res.json("{pass:"+req.body.pass+"}");//response in browser -> "{pass:undefined}"
Could someone tell me why the Node.js server doesn't receive the JSON data?
Have you installed the body-parser
? Otherwise express wont convert the body of the POST request to a JSON object that you can access with req.body.pass

Can I use Passport.js and client-side facebook-authentification?

I'm building a REST-api that uses facebook for authentication, I think the best solution to be platform agnostic is to let the client deal with retrieving an auth-token from facebook and then use that to authenticate, to keep the API as clean as possible.
Is this at all possible?
You can use passport-facebook-token strategy instead of passport-facebook.
In this way you can get the token on the client-side and send it to the application using:
app.post('/auth/facebook/token',
passport.authenticate('facebook-token'),
function(req, res) {
// do something with req.user
res.send(req.user ? 200 : 401);
}
);
With the code above you can pass the token using a query parameter like GET /auth/facebook/token?access_token=<TOKEN_HERE>, putting on the HTTP header access_token or in the request body.

Browsersync middleware: Redirect from a post request

We are using browsersync in development. For a payment confirmation, the payment provider posts back to a predefined url.
The problem is, that Browsersync only displays Cannot POST /payment/success.
So I was thinking about writing a middleware that takes the POST request and redirects to the same URL as a GET request. The problem is, I'm not sure if this is possible with browser sync. When I try to call res.redirect('/') it throws an error:
browserSync.init
server:
baseDir: "...."
index: "/statics/master.html"
middleware: [
modRewrite(['!\\.\\w+$ /statics/master.html [L]']),
(req, res, next) ->
res.redirect('/') # this throws a TypeError: res.redirect is not a function
]
Why is res.redirect not defined? I thought browser sync is based on express?
How could I access the body of the post request? req.body returns undefined
browserSync uses Connect for the middleware and doesn't share the same api as Express it seems.
For a redirect you can at least do the following:
res.writeHead(301, {Location: url});
res.end();
Have a look at their documentation for accessing the request body. The section in Middleware regarding body parsing may yield some results for you.

Resources