当获得URL通过firebase部署-只托管和使用它,它工作得很好,打开网站,但当尝试把api/发送像这样的URL:https://*******.web.app/api/send给我这个错误**
请求的URL在此服务器上找不到。
,然后尝试post url in postman结果显示错误
**JavaScript代码**
const functions = require('firebase-functions');
var {google} = require('googleapis');
var MESSAGING_SCOPE = "https://www.googleapis.com/auth/firebase.messaging";
var SCOPES = [MESSAGING_SCOPE];
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var router = express.Router();
var request = require('request');
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
router.post('/send', function(req, res){
getAccessToken().then(function(access_token){
var title = req.body.title;
var body = req.body.body;
var token = req.body.token;
request.post({
headers:{
Authorization: 'Bearer '+access_token
},
url: "https://fcm.googleapis.com/v1/projects/el-ma3sra/messages:send",
body: JSON.stringify(
{
"message":{
"token" : token,
"notification" : {
"body" : body,
"title" : title,
}
}
}
)
}, function(error, response, body){
res.end(body);
console.log(body);
});
});
});
app.use('/api', router);
function getAccessToken(){
return new Promise(function(resolve, reject){
var key = require("./service-account.json");
var jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
SCOPES,
null
);
jwtClient.authorize(function(err, tokens){
if(err){
reject(err);
return;
}
resolve(tokens.access_token);
});
});
}
exports.api = functions.https.onRequest(app);
firebase.json
{
"hosting": {
"public": "public",
"rewrites":[
{
"source":"/api/send",
"function":"api"
}
],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
我想解决我的错误
我认为您配置了以下API urlhttps://*******.web.app/api/api/send
第一个api来自你的出口(exports.api = ...
)第二个api来自你的express应用程序,你分配你的路由器在api (app.use('/api'...
)启动
可以尝试将路由器分配到根路径
app.use('/', ... )