Express Routes 未解析到端点

Express Routes not resolving to endpoints

我在 Firebase Cloud Functions 上 运行,所以在我的 index.js 中,我有以下代码片段:

const admin = require("firebase-admin");
const functions = require("firebase-functions");
const usersApi = require("./api/users")
const paymentsApi = require("./api/payments")
const express = require('express');
const cors = require('cors');

const app = express();

const checkHeader = async(req, res, next) => {
  if(req.headers.authorization) {
    admin.auth().verifyIdToken(req.headers.authorization)
    .then(token => {
      req.uid = token.uid;
      req.email = token.email;
      req.stripeID = token.stripeID || null;
      return next();
    })
    .catch(e => {
      return next(e.errorInfo.code)
    })
  } else {
      return next('No token found');
  }
}

app.use(cors({origin: true}));
app.use(express.urlencoded({extended: true}));
app.use(express.json());
//app.use(checkHeader);

app.disable("x-powered-by");

app.use("/users", usersApi)
app.use("/payments", paymentsApi)

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

然后在我的 usersApi 路由中:

const express = require('express');
const userRouter = express.Router();
const fb = require('../../fb');
const db = fb.firestore()

userRouter.post('/addUser', (req, res) => {
    return db.collection('users').doc(req.uid).set({
      activeSub: false,
      name: req.body.name
    })
    .catch(err => {
      throw new functions.https.HttpsError('unknown', err.message, {success:false, error: {err}})
    })
})

userRouter.post("*", (req, res) => {
  res.status(404).send("This route does not exist in userRouter");
})

module.exports = userRouter;

没问题。我可以 POST 到域。com/api/users/addUser 我可以访问该功能。

我的付款方式似乎无法解决:

const express = require('express');
const paymentRouter = express.Router();
const functions = require("firebase-functions");
const fb = require('../../fb');
const db = fb.firestore()
const stripe = require('stripe')('.....');


paymentRouter.post('/createSubscription'), (req, res) => {
  return stripe.subscriptions.create({
    customer: req.body.userData.stripeID,
    items: [{plan: req.body.plan}]
  })
  .then((customer) => {
    console.log('then')
    console.log(customer)
    return db.collection('users').doc(req.uid)
    .update({
      stripeID: customer.id,
      activeSub: true
    })
  })
  .catch(e => {
    console.log(e)
    throw new functions.https.HttpsError('unknown', err.message, e);
  })
}

paymentRouter.post('/createUser'), (req, res) => {
  console.log('creating user')
  return stripe.customers.create({
    email: req.email,
    source: req.body.token
  })
  .then(customer => {
    console.log(customer)
    return db.collection('users').doc(req.uid)
    .update({
      stripeID: customer.id,
      activeSub: true
    })
  })
  .catch(e => {
    throw new functions.https.HttpsError('unknown', err.message, e);
  })
}

paymentRouter.post("*", (req, res) => {
  res.status(404).send("This route does not exist in paymentRouter");
})

module.exports = paymentRouter;

每当我向 domain.com/api/payments/createSubscription 或 /createUser 发送 POST 请求时,它总是 returns 404 和 This route does not exist in paymentRouter"

谁能解释为什么?

更改 paymentRouter.post('/createSubscription'), (req, res) => {

paymentRouter.post('/createSubscription', (req, res) => {
  return stripe.subscriptions.create({
    customer: req.body.userData.stripeID,
    items: [{plan: req.body.plan}]
  })
  .then((customer) => {
    console.log('then')
    console.log(customer)
    return db.collection('users').doc(req.uid)
    .update({
      stripeID: customer.id,
      activeSub: true
    })
  })
  .catch(e => {
    console.log(e)
    throw new functions.https.HttpsError('unknown', err.message, e);
  })
}

更改 paymentRouter.post('/createUser'), (req, res) => {

paymentRouter.post('/createUser', (req, res) => {
  console.log('creating user')
  return stripe.customers.create({
    email: req.email,
    source: req.body.token
  })
  .then(customer => {
    console.log(customer)
    return db.collection('users').doc(req.uid)
    .update({
      stripeID: customer.id,
      activeSub: true
    })
  })
  .catch(e => {
    throw new functions.https.HttpsError('unknown', err.message, e);
  })
}