Basically, I have an array that needs to get passed through a function (using JSON). This becomes a module that I can export to a server request. When the request gets "transaction.html", the module should be called upon and the JSON object should print out. I have tried to make this work with no success and wondering what is that I am doing wrong here (new to 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;
And this is the server request below:
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);
In the the transactionmanager module, you have a getTransactions function that doesn't return the values
function getTransactions() {
var values = JSON.parse(arrayValues);
}
then you tried to get back the values
var content = transactions.getTransactions();
You have to raturn the values
function getTransactions() {
var values = JSON.parse(arrayValues);
return values;
}