GET request recieved in php but not node.js w/ Express - node.js

I'm trying to integrate KiwiWall, a rewarded ads/offerwall provider, with my website. The end goal is to credit virtual coins to the user when an offer is completed.
The code that I currently have is as follows. I'm simply trying to make sure a request is being received from KiwiWall, which it is not. It's worth noting that the following DOES log the intended information to the console when a request is sent from my browser or a request testing website like ReqBin:
app.use('/offers/kiwiwall/api', cors(), async (req, res) => {
console.log(req.method)
})
However, no output can be seen when using the postback testing functionality of KiwiWall.
In order to make sure the issue isn't on KiwiWall's end, I hosted the example php code from the KiwiWall docs on the same webserver, and was able to successfully receive and process the request using it. Why is this not working with node.js/Express?

Related

How to structure external API calls for Node.js, express, ejs routing?

//API Call one
function receiveLocation(){
axios({
"method":"GET",
"url":"https://ip-geo-location.p.rapidapi.com/ip/check",
...
})
.then((response)=>{
return response.data.country.name;
})
.catch((error)=>{
console.log(error)
})
}
//API Call two
//API Call three
console.log(receiveLocation());
app.get("/", function(req,res){
var location = receiveLocation();//
//Then render all the data from my API calls such as location, currency,
//etc. in my landing page. Also use that data on the backend.
res.render("landing",{location:location});
});
I am currently trying to make a website that uses multiple API calls to get information such as location, currency, and other things of a user who loads the website.
I am attempting to receive all the information from the different API calls as I go through the get request route that allows a user to see the landing page. Firstly, I am not even sure if this is allowed. If it is allowed/standard practice, what am I doing wrong in this example. I am attempting to call a function that in the get route to the root page that returns the country of a visiting user. But after doing some console.log() debugging I see that that information is never being received in the get route. Last note: I want to use the info from the API calls both to change what the user sees, and for some calculations that would need to be run on the backend.
If this is not allowed/not standard practice, may someone explain what I should do instead/point in the right direction as to what I should learn to get a better understanding of what I am trying to do
What you are trying at the moment to do is not standard practice, you should lookup MVC for Express,there you will learn how to structure your backend code so that the GET Routes will be used as Views ( these will be your server getting some public files like html,css,javascript that will be passed some information from the Controllers,this is done by using some server-side renders like EJS).
I recommend taking this Udemy course https://www.udemy.com/course/nodejs-the-complete-guide/ for a full understanding,but if you don't have the time,lookup node.js mvc with express, there is plently information about this.

Unable to get redirect response from Node server using res.writeHead() and .end()

I'm writing a server in Node.js. I've been using the send-data/json package to send responses, with much success. For example, at the end of my API call I use this code:
sendJson(res, res, {some: content})
This works great. However, I have a need to implement a URL redirect in one of my API endpoints. The code I am seeing everywhere to do this looks like this:
res.writeHead(302, {Location: 'http://myUrl.com'})
res.end()
However, this change causes my server to stop sending responses on this endpoint.
What am I missing?
Note: this is vanilla Node without Express.
Update: to clarify, based on contributions in the comments, this is the result of a GET request, so I expect that redirects should be enabled by default. I am still curious why no response is coming back from the server at all, regardless of whether it is an erroneous redirect attempt or a successful one.

How to Get Current User in Backend with Firebase in Node.js?

I am so confused,
all the Firebase authentication tutorial online are teaching how to login in frontend,
of course you can get the token and send it to server for verification if its a post request,
but what about normal get request like a page request? I have installed firebase-admin already but i didnt find any method for getting current user........
i am using node and express
for example
app.get('/', function(req, res) {
const idToken = (where can i get the token??)
console.log(idToken);
res.render('index.ejs');
});
You still have to arrange for the auth token to be sent in the HTTP request. You can do that with a header in the request. Sample code showing exactly this case can be found in the official samples. This will work for any HTTP method, and is a lot better than trying to use a POST body.
The sample uses the Authorization header to transmit the token and verifyIdToken() to make sure it's valid.

Sending new/mutated data back to the page in the same POST request?

Playing around with React and Node/Express what I have is a form with a POST request that sends some state information back to the server, on the server side I mutate/change the state and then send the state back in the same post without a redirect() or a page refresh.
router.post('/ProjectPost', function(req, res, next){
console.log('postin up');
const transform = req.body.incom += 'neewer'
console.log(transform);
res.json({backAtChew : transform});
})
Is this ok? Is it ideal? Does in only work here because of the small amount of data changes? Is this ok for scaling? Having the least amount of major page changes or refreshes is ideal for a SPA so this doesn't seem bad. In my actual application it will be major SQL calls or Oauthentication or API usage or what have you have you so I wanted to make sure before getting the idea that this was a way to go.
The calls to the server are made with axios.js

Nodejs website execution in the background

So I am developing a call logging website based on Nodejs, I have come to a point from where I need to send SMS to the engineer when the call is logged. I have got access to the SMS gateway which is essentially a URL like the following :
http://192.x.x.x username=x password=y&to=mobileno&text=xyz
My requirement is to execute this link so that the SMS is sent and the page remains as it was without having to be redirected.
Many thanks
This is a pretty simple task due to node.js io asynchrony.
Suppose you have an express route:
app.get('/some-url', (req, res) => {
request('http://192.x.x.x?username=x&password=y&to=mobileno&text=xyz')
.then(response => { // process response })
res.end();
});
What i understood from your description is you have to hit the url after certain task.So u can use a rest client to hit the url in the callback of certain task(here after call is logged).
I prefer node-rest-client(https://www.npmjs.com/package/node-rest-client).There are other options also.

Resources