Self-hosted Parse Platform RESTful API access - node.js

I'm using Parse for some quick MVP app development to prove some concepts. Everything is working fine and I'm simply trying to extend the abilities of our app by setting up some 3rd party integrations etc.
I would like to access my Cloud Code functions via the RESTful api, as documented here
I've changed the server address, but have no luck connecting and pulling the data.
I am using Node.js to connect to the REST api, which is being hosted on Heroku.
var options = {
host: 'https://###############.herokuapp.com',
port: 443,
path: '/parse/functions/'+req.param('text'),
method: 'POST',
headers: {
'X-Parse-Application-Id': '##############',
'X-Parse-REST-API-Key': '###########'
}
};
https.get(options, function(resp){
resp.on('data', function(chunk){
//do something with chunk
res.send(chunk)
});
}).on("error", function(e){
console.log("Got error: " + e.message);
res.send(e)
});
So far no luck. Any suggestions?

Hard to tell what exactly is wrong without seeing the Parse Server configuration, the Cloud Code function, and more importantly the server's response, but here's what worked for me:
curl -X POST -H "X-Parse-Application-Id: XYZUASDASDA" -H
"X-Parse-REST-API-Key: XYZUASDASDA" -H "Content-Type:
application/json" -d '{}'
http://localhost:3000/parse/functions/averageStars
Result: {"result":"Hello"}
Here's my test Cloud Code function:
Parse.Cloud.define('averageStars', function(request, response) {
response.success('Hello'); });
As a quick test, I suggest:
Try using CURL
Try the test function above

Related

digest auth with file upload in electron (superagent, urllib)

I have an Electron based application and it can discover certain onvif based devices. For a certain upload endpoint, the authentication is done with digest.
There are two processes running with this electron: node and chromium.
In the chromium process, I have urllib trying to do the digest authentication.
return new Promise((resolve, reject) => {
urllib.request(uploadUrl, {
method: 'POST',
files: {
filename: fs.readFileSync(filePath)
},
digestAuth: `${device.username}:${device.password}`
}).then((response) => {
log.info('apk uploaded', response)
return resolve(response.status)
}).catch((error) => {
log.info('error uploading apk', error)
})
})
This always returns:
Error: read ECONNRESET at TCP.onStreamRead (internal/stream_base_commons.js:209:20)
However, I also tried it with curl and it works perfectly with that
curl -v http://upload.url.62.62/upload \
--digest \
-u "username:passsword" \
--form filename=#"app.apk"
I thought it might be the Chromium process that makes things worse with digest. So I tried to do it in the main process with ipcHandler and it's the same error.
I'm not sure how can I translate this curl command into a node code.
I've tried superagent, urllib, axios-digest (using in node main process).
Superagent gives a different error
Error: Request has been terminated Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.
axios-digest gives the error: reply was never sent
At this point I'm clueless on how to make this work.

Why does my node.js HTTP request fail with code 501?

I have been trying to call a web service running in Docker on my machine on port 4801. I can access the service in the browser, via curl and via .NET's HttpClient, but if I try from node.js (either using axios or the native http module) the request fails with status code 501 Not Implemented.
I eventually tracked the problem down to the fact that there is another process, called ServiceLayer.exe (description: "Logitech VC ServiceLayer"), listening on port 4801. How is Docker able to expose my service on that port such that it can be accessed by the methods listed above, but not from node?
Here is a minimal repro:
const axios = require("axios");
axios.get("http://localhost:4801")
.then(response => console.log(response.data))
.catch(error => console.log({
status: error.response.status,
headers: error.response.headers
}));
docker run -p 4801:8000 -d crccheck/hello-world
node test.js
Output:
{
status: 501,
headers: { server: 'websocket-sharp/1.0', connection: 'close' }
}
I am guessing the websocket-sharp bit is potentially significant.

How do I connect React native app with node js?

I have already created backend using node js for sigin & signup page. Now I want to connect to node js . But i have no idea how to do that. I want to connect both react native with my node js. Can you help me ?
simply as how we do for web apps.
here is an example of error reporting
export default async function (body) {
console.log(JSON.stringify(body))
const res = await fetch(`${host}/api/report`, {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
},
})
const { message } = await res.json()
if (message) return Toast({ message: message });
else return Toast({ message: 'network error' });
}
I have used fetch to send a POST request to my nodejs server
use API tool like postman or other and make your your nodejs APIs works fine and then connect to your React Native app as above.
You can use ngrok to connect Node with react-native. Run this command:
npm i ngrok -g # installing it globally
Then open another terminal. Run:
ngrok http 3000 # the port you are running on node
Then it will show an alternative link that you can use to test with your Node.
Note: if ngrok http 3000 doesn't work, try ngrok http -region us 3000.
The available ones are us, eu, ap, and au. In my case eu worked for me.
Then copy the link generated e.g. http://8074-129-205-124-100.eu.ngrok.io and test your backend if it displays APIs.
If the link works then you can use it with fetch. Uploading json data to send to MongoDB as the case maybe.

curl command equivalent in nodejs

I have the curl command here which is working as I want:
curl -X POST -H "Content-Type: application/json" -d #cats.json http://localhost:8080
And I would like to get help on translating this into nodejs
I currently have localhost set up and my goal is to display data stored in a JSON file. I currently copy and paste the data into postman and post the data to the localhost but I want to not use postman. The curl command was useful but I want to incorporate it into my code.
I've tried
var request = require('request');
request.post({
headers: {'content-type' : 'application/json'},
url: 'http://localhost/8080',
body: "someData"
}, function(error, response, body){
console.log(body);
});
I've tried this but it doesn't provide the same outcome as the curl command does where the data appears in my localhost
In postman, you can export NodeJS request code out:
Click on Code
Then select NodeJS -> Request.
Copy to Clipboard

How to debug request-promise which fail with ETIMEDOUT

I'm running Debian 6 with NodeJS6 and request-promise.
I'm using request-promise to deliver web hooks. Currently, we are delivering around 2+M web hooks each day.
Most of the time everything runs fine, but there are cases where the HTTP request simply isn't delivered.
This is my simple code:
requestPromise({ url: 'https://change-iot-request/do', auth: { user: "switch", pass: "my-password" }, method: "GET", timeout: 15000, rejectUnauthorized: false})
.catch(function(err) {
console.log(err);
});
Sometimes the endpoint simply doesn't receive anything.
I've tried to run tshark to see if packages came through - nothing on any of the servers.
If I when it occurs run curl command with the same parameters the request DOES come through.
And if I restart the NodeJS it starts to deliver correctly again.
This is the error output:
{"name":"RequestError","message":"Error: ETIMEDOUT","cause":{"code":"ETIMEDOUT","connect":true},"error":{"code":"ETIMEDOUT","connect":true}
What to do?
Especially when curl does work so the connection should be fine.
it can be a lot of causes, maybe it's not node fold at all
ETIMEDOUT means that your server do not get response for request
you can try set a timeout, something like
var rp = require('request-promise');
var reqPromise = rp({
method: 'get',
uri: 'http://localhost:8080/test-connection-length',
timeout: 600000, // 10 min.
resolveWithFullResponse: true
});
or install node-retry and your server will re-run automatically when it's needed

Resources