Last active
August 21, 2023 16:46
-
-
Save isaumya/0081a9318e4f7723e0123f4def744a0e to your computer and use it in GitHub Desktop.
Making HTTP CRUD requests with Firebase Cloud Functions using node request-promise package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require('firebase-functions'); | |
const cors = require('cors')({ origin: true }); | |
const rp = require('request-promise'); | |
exports.checkIP = functions.https.onRequest((req, res) => { | |
cors(req, res, () => { | |
if( req.method !== "GET" ) { | |
return res.status(401).json({ | |
message: "Not allowed" | |
}); | |
} | |
rp({ | |
uri: 'https://api.ipify.org', | |
qs: { | |
format: 'json' | |
}, | |
headers: { | |
'User-Agent': 'Request-Promise', | |
'Connection': 'keep-alive' | |
}, | |
json: true // Automatically parses the JSON string in the response | |
}) | |
.then(data => { | |
console.log(data) | |
return res.status(200).json({ | |
ip: data.ip | |
}) | |
}) | |
.catch(err => { | |
console.log(err) | |
return res.status(500).json({ | |
message: err | |
}) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment