我正在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;
没问题我可以发布到domain.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;
每当我将POST请求发送到domain.com/api/payments/createSubscription或/ createUser时,它总是使用
This route does not exist in paymentRouter"
返回404谁能解释为什么?
最佳答案
更改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);
})
}
关于javascript - Express路由无法解析到端点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51458634/