Я создаю Line Bot, запустив Node.js в Firebase, но он не работает в моей локальной среде.
Когда "firebase serve - only functions, хостинг" выполняется и "Доступ к http: // localhost: 5000 / webhook ", отображается сообщение" Cannot GET / xxx / us-central1 / app / webhook ".
index.ts
'use strict';
const functions = require('firebase-functions');
const express = require('express');
const line = require('@line/bot-sdk');
const config = {
channelSecret: 'channelSecret',
channelAccessToken: 'channelAccessToken'
};
const app = express();
app.post('/webhook', line.middleware(config), (req: any, res: any) => {
console.log(req.body.events);
Promise
.all(req.body.events.map(handleEvent))
.then((result) => res.json(result))
.catch((result) => console.log('error!!!'));
});
const client = new line.Client(config);
async function handleEvent(event: any) {
if (event.type !== 'message' || event.message.type !== 'text') {
return Promise.resolve(null);
}
return client.replyMessage(event.replyToken, {
type: 'text',
text: event.message.text
});
}
exports.app = functions.https.onRequest(app);
firebase. json
{
"hosting": {
"public": "./",
"rewrites": [{
"source": "/webhook",
"function": "app"
}],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
package.json
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase serve --only functions --project golfure",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "8"
},
"main": "lib/index.js",
"dependencies": {
"@line/bot-sdk": "^6.8.2",
"express": "^4.17.1",
"firebase-admin": "^8.6.0",
"firebase-functions": "^3.3.0",
"ngrok": "^3.2.5"
},
"devDependencies": {
"tslint": "^5.12.0",
"typescript": "^3.2.2",
"firebase-functions-test": "^0.1.6"
},
"private": true
}
Как это будет работать? Спасибо.