【问题标题】:how can I solve a Cannot Get / error nodejs如何解决无法获取/错误 nodejs
【发布时间】:2020-10-21 13:37:55
【问题描述】:
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const cors = require("cors");

app.use(cors());
app.use(bodyParser.json());

const bugs = [
  { id: 1, description: "Bug 1", userId: 1, resolved: true },
  { id: 2, description: "Bug 2", userId: 1 },
  { id: 3, description: "Bug 3", userId: 2 },
  { id: 4, description: "Bug 4" }
];

app.get("/api/bugs", (req, res) => {
  res.json(bugs);
});

app.post("/api/bugs", (req, res) => {
  const bug = { id: Date.now(), resolved: false, ...req.body };
  bugs.push(bug);

  res.json(bug);
});

app.patch("/api/bugs/:id", (req, res) => {
  const index = bugs.findIndex(bug => bug.id === parseInt(req.params.id));
  const bug = bugs[index];
  if ("resolved" in req.body) bug.resolved = req.body.resolved;
  if ("userId" in req.body) bug.userId = req.body.userId;

  res.json(bug);
});

app.listen(8000, () => {
  console.log("Node server started on port 8000.");
});

我对nodejs一无所知,但我正在关注别人的教程。他们想将一些后端与项目集成。每当我启动服务器时,我都会收到此错误。我该怎么办?

【问题讨论】:

  • 这个网址怎么样? http://localhost:8080/api/bugs

标签: node.js server backend


【解决方案1】:

您的前端似乎调用“http://localhost:8000”,但此路由未定义。

前端应该调用“http://localhost:8000/api/bugs”

【讨论】:

    猜你喜欢
    • 2014-02-14
    • 2021-03-13
    • 1970-01-01
    • 2017-09-04
    • 2018-08-03
    • 2012-10-31
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多