After starting the server with node server.js I'm getting an error stating Cannot GET when I access localhost:3000 and I just wondered if anyone knew how to fix it? Please tell me where I'm doing wrong? Ps. I'm using CodeSandbox
Thank you.
const express = require("express");
const line = require("@line/bot-sdk");
const axios = require("axios");
const { Configuration, OpenAIApi } = require("openai");
const config = {
channelAccessToken: process.env.YOUR_CHANNEL_ACCESS_TOKEN,
channelSecret: process.env.YOUR_CHANNEL_SECRET
};
const client = new line.Client(config);
const app = express();
app.post("/webhook", line.middleware(config), async (req, res) => {
const event = req.body.events[0];
if (event.type === "message" && event.message.type === "text") {
const message = event.message.text;
const response = await openaiRequest(message);
const reply = {
type: "text",
text: response
};
client.replyMessage(event.replyToken, reply);
}
});
async function openaiRequest(message) {
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY
});
const openai = new OpenAIApi(configuration);
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: message
}
]
});
console.log(JSON.stringify(completion.data));
console.log(completion.data);
return completion.data.choices[0].message.content;
}
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});