bash
mkdir firebase
cd firebase
npm install -g firebase-tools
firebase init
firebase login
- ロケーションを設定
index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const axios = require('axios');
admin.initializeApp();
const db = admin.firestore();
exports.db = functions.https.onRequest(async (request, response) => {
const snapshot = await db.collection('users').get();
const items = snapshot.docs.map(doc => doc.data());
console.log(items);
response.send(JSON.stringify(items));
});
exports.query = functions.https.onRequest((request, response) => {
console.log('query', request.query);
response.send(JSON.stringify(request.query));
});
exports.html = functions.https.onRequest((request, response) => {
console.log('query', request.query);
response.status(200).send(
`<html>
<body>
<div style='color: red'>${JSON.stringify(request.query)}</div>
</body>
</html>`
);
});
exports.schedule = functions.pubsub.schedule('every 1 minutes').onRun((context) => {
console.log('every 1 minutes');
return null;
});
exports.axios = functions.https.onRequest(async (request, response) => {
const { data } = await axios.get('https://xxx.com/query?id=1');
console.log(data);
response.send(JSON.stringify(data));
});
bash
firebase deploy --only functions:query,functions:html