将模块导出到读取 JSON 内容的服务器请求
Exporting a module to a server request that reads JSON content
基本上,我有一个需要通过函数传递的数组(使用 JSON)。这成为我可以导出到服务器请求的模块。当请求获得“transaction.html”时,应该调用模块并打印出 JSON 对象。我试图使这项工作没有成功,并且想知道我在这里做错了什么(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;
下面是服务器请求:
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);
在 transactionmanager
模块中,您有一个 getTransactions
函数 return values
function getTransactions() {
var values = JSON.parse(arrayValues);
}
然后你试图取回值
var content = transactions.getTransactions();
你必须 raturn values
function getTransactions() {
var values = JSON.parse(arrayValues);
return values;
}
基本上,我有一个需要通过函数传递的数组(使用 JSON)。这成为我可以导出到服务器请求的模块。当请求获得“transaction.html”时,应该调用模块并打印出 JSON 对象。我试图使这项工作没有成功,并且想知道我在这里做错了什么(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;
下面是服务器请求:
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);
在 transactionmanager
模块中,您有一个 getTransactions
函数 return values
function getTransactions() {
var values = JSON.parse(arrayValues);
}
然后你试图取回值
var content = transactions.getTransactions();
你必须 raturn values
function getTransactions() {
var values = JSON.parse(arrayValues);
return values;
}