大家好,我试图构建一个小型应用程序,但不幸的是,最后我在下面遇到此错误。我在这里看到了其他主题,但与我的主题不同。我该怎么做才能解决这个问题?非常感谢。
import express from "express";
import { readFileSync } from "fs";
const app = express();
const meals = JSON.parse(readFileSync(__dirname + "/data/meals.json"));
const reviews = JSON.parse(readFileSync(__dirname + "/data/reviews.json"));
const reservations = JSON.parse(
readFileSync(__dirname + "/data/reservations.json")
);
app.get("/", (req, res) => res.send("Can't find that one"));
///=== Respond with the json for all the meals For each meal,
//=== if there are reviews matching it's meal ID
app.get("/meals", (req, res, next) => {
meals.forEach(meal => {
meal.reviews = [];
for (let review of reviews) {
if (meal.id === review.mealId) {
meal.reviews.push(review);
}
}
});
res.json(meals);
next();
});
//==== Respond with the json for all the meals (including it's reviews) that are cheap ====
app.get("/cheap-meals", (req, res, next) => {
res.send(filter(meal => meal.price <= 64));
next();
});
//=== Respond with the json for all the meals (including it's reviews) that can fit lots of people
app.get("/large-meals", (req, res, next) => {
res.send(filter(meal => meal.maxNumberOfGuests >= 10));
next();
});
//==== Respond with the json for all reservations ====
app.get("/reservations", (req, res, next) => {
res.send(reservations);
next();
});
//==== Respond with the json for a random reservation ====
app.get("/reservation", (req, res, next) => {
res.send(reservations[Math.floor(Math.random() * length)]);
next();
});
app.listen(8080, () => console.log("Magic happens on port " + 8080));
下面的这些代码有效,我想使用上面的代码段。
const express = require("express");
const fs = require("fs");
const app = express();
const meals = JSON.parse(fs.readFileSync(__dirname + "/data/meals.json"));
const reviews = JSON.parse(fs.readFileSync(__dirname + "/data/reviews.json"));
const reservations = JSON.parse(
fs.readFileSync(__dirname + "/data/reservations.json")
);