GET konnte nicht abgerufen werden/Fehler – GET http://localhost:3000/ 404 (nicht gefunden)
P粉236743689
P粉236743689 2024-03-27 14:13:04
0
1
281

Ich erhalte in meinem Browser die Meldung „Unable to get /“, wenn ich Node und express.js verwende. Von der Konsole erhalte ich Folgendes: Die Ressource konnte nicht geladen werden: Der Server hat mit dem Status 404 (Nicht gefunden) geantwortet. Der folgende Code funktioniert sowohl für Server als auch für Browser. Ich brauche wirklich Hilfe, da ich nicht verstehen kann, was mit diesem Code nicht stimmt. Im Grunde mache ich einen Front-End-Entwicklungs-Nano-Abschluss bei Udacity und folge den Übungen. Diese Übung ist mir nicht gelungen.

Code for the server:
    /* Empty JS object to act as endpoint for all routes */
    projectData = {};

    /* Express to run server and routes */
    const express = require("express");

    /* Start up an instance of app */
    const app = express();

    /* Dependencies */
    const bodyParser = require("body-parser");
    /* Middleware*/
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(bodyParser.json());
    const cors = require("cors");
    app.use(cors());

    /* Initialize the main project folder*/
    app.use(express.static("website"));

    const port = 3000;
    /* Spin up the server*/
    const server = app.listen(port, listening);
    function listening() {
      // console.log(server);
      console.log(`running on localhost: ${port}`);
    }

    // GET route
    app.get("/all", sendData);

    function sendData(request, response) {
      response.send(projectData);
    }

    // POST route
    app.post("/add", callBack);

    function callBack(req, res) {
      res.send("POST received");
    }

    // POST an animal
    const data = [];

    app.post("/animal", addAnimal);

    function addAnimal(req, res) {
      data.push(req.body);
    }

   code for the browser
  /* Function to POST data */
  const postData = async (url = "", data = {}) => {
    console.log(data);
    const response = await fetch(url, {
      method: "POST", // *GET, POST, PUT, DELETE, etc.
      credentials: "same-origin", // include, *same-origin, omit
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(data), // body data type must match "Content-Type" header
    });

    try {
      const newData = await response.json();
      // console.log(newData);
      return newData;
    } catch (error) {
      console.log("error", error);
      // appropriately handle the error
    }
  };

  //Call Function
  postData("addAnimal", { animal: "lion" });

P粉236743689
P粉236743689

Antworte allen(1)
P粉738046172

当服务器接收到对其没有定义的处理程序 / 存在的路由的请求时,就会发生“无法获取 /”错误。

现在你有了这个:

    // GET route
    app.get("/all", sendData);

    function sendData(request, response) {
      response.send(projectData);
    }
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!