Básicamente, tengo una matriz que debe pasar a través de una función (usando JSON). Esto se convierte en un módulo que puedo exportar a una solicitud del servidor. Cuando la solicitud obtiene "transaction.html", se debe llamar al módulo y se debe imprimir el objeto JSON. He intentado hacer que esto funcione sin éxito y me pregunto qué es lo que estoy haciendo mal aquí (¡nuevo en JS!)
//this is becomes the transactionmanager module var arrayValues = [ { date: "April 3, 2021", description: "House", category: "Mortgage", amount: 1500 }, { date: "March 7, 2022", description: "Duke Energy", category: "Bills and utilities", amount: 200 }, { date: "January 24, 2022", description: "Publix", category: "Shopping", amount: 120 }, { date: "May 15, 2022", description: "AMC", category: "Entertainment", amount: 20 } ]; function getTransactions() { var values = JSON.parse(arrayValues); } exports.getTransactions = getTransactions;Y esta es la solicitud del servidor a continuación:
var http = require("http"); var url = require("url"); var transactions = require("transactionmanager"); var server = http.createServer(function (req, res) { if (req.url == "/index.html") { res.writeHead(200, {"Content-type": "text/html"}); res.write("<html><body><p>Welcome!</p></body></html>"); res.end(); } else if (req.method == "GET" && req.url == "/transactions.html") { res.writeHead(200, {"Content-type": "text/html"}); res.write("<html><body><p>Transaction History</p></body></html>"); var content = transactions.getTransactions(); res.end(content); } else { res.writeHead(404, {"Content-type": "text/html"}); res.end("<html><body><p>Sorry, the page you are looking for is not here</p></body></html>"); return; } }); server.listen(3000);En el módulo de administrador de transactionmanager , tiene una función getTransactions que no devuelve los values
function getTransactions() { var values = JSON.parse(arrayValues); }luego intentaste recuperar los valores
var content = transactions.getTransactions(); Tienes que cambiar los values
function getTransactions() { var values = JSON.parse(arrayValues); return values; }