Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

node.js - Showing static JSON file content in html using express

im trying to show the content of my JSON file in html using express what I have is this

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

but I have no idea how to pass that to html , im trying to use pug , but im really new to that too

thanks a lot

question from:https://stackoverflow.com/questions/65908440/showing-static-json-file-content-in-html-using-express

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Put this code in your ./src/views/index.pug

index.pug

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...