【问题标题】:Showing static JSON file content in html using express使用 express 在 html 中显示静态 JSON 文件内容
【发布时间】:2021-04-30 15:44:12
【问题描述】:

我试图在 html 中显示我的 JSON 文件的内容,使用 express 我所拥有的是这个

const data = require("./data.json");
const express = require("express");
const morgan = require("morgan");
var pug = require("pug");
//import mongoose from "mongoose";
//import port from "./config/config";
//const rutas = require("./routes/index.js");
//const routes = require("./routes");
console.log(data);

const app = express();
var path = require("path");
app.use(morgan("dev"));
app.set("view engine", "pug");
app.set("views", "./src/views");

app.set("port", process.env.PORT || 3000);
// levantar servidor
app.use(express.json()); // for parsing application/json
app.use(express.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencodedapp.set("port", process.env.PORT || _port);
app.use(morgan("dev"));
var datos = [];
datos = JSON.stringify(data);
var newEventList = data.map((data) => ({
  id: data.id,
  nombre: data.nombre,
  categoria: data.categoria,
  chef: data.chef,
  Ingredientes: data.ingredientes.nombre,
  cantidad: data.ingredientes.cantidad,
  preparacion: data.preparacion,
}));

app.get("/", function (req, res) {
  res.render("index", { title: "Hey", message: "Hello there!", newEventList });
});

但我不知道如何将其传递给 html,我正在尝试使用 pug,但我对此也很陌生

非常感谢

【问题讨论】:

    标签: node.js json pug


    【解决方案1】:

    将此代码放入您的./src/views/index.pug

    index.pug

    title=title
    p=message
    each key in newEventList
       p #{newEventList[key]}
    

    【讨论】:

    • 非常感谢,现在我只收到消息“hello there”,但没有 JSON 数据,我的 pug 文件是这样写的 doctype html html(lang='en') head title=title p=消息 newEventList p #{newEventList[key]} 中的每个键
    猜你喜欢
    • 1970-01-01
    • 2014-07-24
    • 2014-09-19
    • 2018-05-17
    • 2018-10-07
    • 2021-11-23
    • 1970-01-01
    • 2018-05-28
    • 2023-03-11
    相关资源
    最近更新 更多