【问题标题】:Firebase + Express + RoutesFirebase + Express + 路由
【发布时间】:2018-07-12 14:31:06
【问题描述】:

我从 Firebase 开始并尝试实现一个 api,但我在尝试使用 Express 创建一些路由时遇到了麻烦。关注:

firebase.json

{
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [{
      "source": "/api/**",
      "function": "api"
    },{
      "source": "/api/auth/**",
      "function": "api"
    }]
  },
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint"
    ]
  }
}

index.js

const functions = require("firebase-functions");
const cors = require("cors");
const express = require("express");
const api = express();

api.use(cors({ origin: true }))

api.use("/auth", require("./routes/auth"));

exports.api = functions.https.onRequest(api);

路由/auth.js

var express = require("express");

module.exports = () => {

    var auth = express.Router();

    auth.post("/signin", (request, response) => {
        var service = require("../services/auth/signin");
        service(request, response);
    });

    return auth;

}

services/auth/signin.js

module.exports = (request, response) => {
    response.status(200).send({
        "success": true,
        "request": request.body.email
    });
}

我不确定它是否是最好的框架以及模块是否正确实施。任何建议都非常受欢迎。谢谢!

【问题讨论】:

    标签: firebase express google-cloud-functions firebase-hosting


    【解决方案1】:

    对于端点是 /api/auth/signin 尝试

    api.use("/api", require("./routes/auth"));
    

    auth.post("/auth/signin", (request, response) => {
    

    然后删除

    ,{
      "source": "/api/auth/**",
      "function": "api"
    }
    

    有点反直觉,因为它似乎映射 /api 两次,但这在几个应用程序中对我有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-30
      • 2017-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多