mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2026-03-10 13:15:05 +00:00
Set up Express for API calls.
This commit is contained in:
@@ -1,22 +1,43 @@
|
||||
/* eslint-disable promise/always-return */
|
||||
const functions = require('firebase-functions');
|
||||
const admin = require('firebase-admin');
|
||||
|
||||
const app = require('express')();
|
||||
admin.initializeApp();
|
||||
// // Create and Deploy Your First Cloud Functions
|
||||
// // https://firebase.google.com/docs/functions/write-firebase-functions
|
||||
//
|
||||
exports.helloWorld = functions.https.onRequest((request, response) => {
|
||||
response.send("Hello from Firebase!");
|
||||
});
|
||||
|
||||
exports.getUsername = functions.https.onRequest((req, res) => {
|
||||
admin.firestore().collection('users').get()
|
||||
.then(data => {
|
||||
const firebaseConfig = {
|
||||
apiKey: "AIzaSyCvsWetg4qFdsPGfJ3LCw_QaaYzoan7Q34",
|
||||
authDomain: "twistter-e4649.firebaseapp.com",
|
||||
databaseURL: "https://twistter-e4649.firebaseio.com",
|
||||
projectId: "twistter-e4649",
|
||||
storageBucket: "twistter-e4649.appspot.com",
|
||||
messagingSenderId: "20131817365",
|
||||
appId: "1:20131817365:web:633c95fb08b16d4526b89c"
|
||||
};
|
||||
const firebase = require('firebase');
|
||||
firebase.initializeApp(firebaseConfig);
|
||||
|
||||
app.get('/getUsers', (req, res) => {
|
||||
admin.firestore().collection('users').get().then(data => {
|
||||
let users = [];
|
||||
data.forEach(doc => {
|
||||
users.push(doc.data());
|
||||
})
|
||||
return res.json(users);
|
||||
})
|
||||
.catch((err) => console.error(err));
|
||||
})
|
||||
}); return res.json(users);
|
||||
}).catch((err) => console.error(err));
|
||||
});
|
||||
|
||||
app.post('/postUser', (req, res) => {
|
||||
const newUser = {
|
||||
body: req.body.body
|
||||
};
|
||||
admin.firestore().collection('users').add(newUser).then((doc) => {
|
||||
res.json({
|
||||
message: 'Successfully added!'
|
||||
});
|
||||
}).catch((err) => {
|
||||
res.status(500).json({
|
||||
error: "Error in posting user!"
|
||||
});
|
||||
console.error(err);
|
||||
});
|
||||
});
|
||||
exports.api = functions.https.onRequest(app);
|
||||
949
functions/package-lock.json
generated
949
functions/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -13,6 +13,7 @@
|
||||
"node": "8"
|
||||
},
|
||||
"dependencies": {
|
||||
"firebase": "^6.6.1",
|
||||
"firebase-admin": "^8.0.0",
|
||||
"firebase-functions": "^3.1.0"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user