-1

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.

Cannot GET/

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}`);
});

1 Answers1

1

Your app is functioning correctly. But you see this error because you try to load https://something.codesandbox.io and you don't have any route defined at GET /.

You can read more about routing in Express over in the official guide.

Adding a simple route could be done with the following code.

app.get('/', (req, res) => {
  res.send('hello world')
})
picsoung
  • 6,314
  • 1
  • 18
  • 35