My current setup involves a basic koa
REST service written in typescript. It consists of a simple GET
request that aims to retrieve all users from a firebase database located at db reference /user
.
Upon receiving the request, the application successfully obtains the database reference but seems to be stuck indefinitely when attempting to fetch the actual value.
Here is an excerpt from my index.ts
file:
import * as Koa from "koa";
import * as Router from "koa-router";
import * as json from "koa-json";
import * as bodyParser from "koa-bodyparser";
import firebase from "firebase";
import { config } from "./config";
const app = new Koa();
const router = new Router();
router.get("/users", async (ctx, next) => {
firebase.initializeApp(config.firebaseConfig);
const db = firebase.database();
const ref = db.ref("user");
console.log("got ref");
const snapshot = await ref.once("value");
console.log("got snapshot");
ctx.body = { users: snapshot.val() };
await next();
});
app.use(json());
app.use(bodyParser());
app.use(router.routes()).use(router.allowedMethods());
app.listen(3000, () => {
console.log("Koa started on port 3000");
});
Output generated by the application:
Koa started on port 3000
<-- GET /users
got ref
[2021-02-21T10:09:03.818Z] @firebase/database: FIREBASE WARNING: Namespace [masked-my-project]-dev-default-rtdb lives in a different region. Please change your database URL to https://[masked-my-project]-dev-default-rtdb.europe-west1.firebasedatabase.app (https://[masked-my-project]-dev-default-rtdb.firebaseio.com/)
This is the content of my tsconfig.json
:
{
"version": "2.0.2",
"compilerOptions": {
"outDir": "./dist",
"lib": ["ES5", "ES6"],
"noImplicitReturns": true,
"sourceMap": true,
"target": "ES6",
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
"include": ["./src/**/*"],
"files": ["node_modules/typescript/lib/lib.es6.d.ts"],
"exclude": ["node_modules"]
}
Details from package.json
:
{
"name": "koa-new",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"dev": "./node_modules/.bin/tsc-watch --onSuccess \"node .\"",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^8.2.0",
"firebase": "^8.2.9",
"koa": "^2.13.1",
"koa-bodyparser": "^4.3.0",
"koa-json": "^2.0.2",
"koa-logger": "^3.2.1",
"koa-route": "^3.2.0",
"koa-router": "^10.0.0",
"ts-node": "^9.1.1",
"typescript": "^4.1.5"
},
"devDependencies": {
"@types/dotenv": "^8.2.0",
"@types/firebase": "^3.2.1",
"@types/koa": "^2.13.0",
"@types/koa-bodyparser": "^4.3.0",
"@types/koa-json": "^2.0.18",
"@types/koa-logger": "^3.1.1",
"@types/koa-router": "^7.4.1",
"@types/node": "^14.14.31",
"tsc-watch": "^4.2.9"
}
}
Contents of .env
:
FIREBASE_API_KEY=[mask]
FIREBASE_AUTH_DOMAIN=[mask].firebaseapp.com
FIREBASE_DATABASE_URL=https://[mask]-default-rtdb.europe-west1.firebasedatabase.app
FIREBASE_PROJECT_ID=[mask]
FIREBASE_STORAGE_BUCKET=[mask].appspot.com
FIREBASE_MESSAGING_SENDER_ID=[mask]
FIREBASE_APP_ID=1:[mask]:web:[mask]
FIREBASE_MEASUREMENT_ID=[mask]