在下面的代码中,将信息从app.js传递给choices.js的最佳方法是什么? 目前,我将它们全部放在一个名为answer的变量中。 这是最好的方法吗? 谢谢。
这些是app.js中的代码:
var express = require("express");
var cors = require("cors");
var bodyParser = require("body-parser");
var app = express();
var sql = require('msnodesqlv8');
:
var choicesTerm = [];
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(function (req, res, next) {
console.log(`${req.method} request for '${req.url}' -
${JSON.stringify(req.body)}`);
next();
});
var connStr = "Driver={SQL Server Native Client 11.0};....";
sql.open(connStr, function (err, conn) {
var pm = conn.procedureMgr();
pm.callproc('mySP',sessionID, function (err, results) {
:
pm.callproc('mySp2', function (err, results) {
:
if (results.length)
{
var row = results[0];
var Answers = [];
Answers = row.Answers.split("|");
for (var i = 0, len = Answers.length; i < len; i++) {
var answer = {value: Answers[i], description: Answers[i], question: row.text, detail: row.detail}; //--> pass information to choices.js
choicesTerm[i] = answer;
}
}
})
});
});
app.use(express.static("./public"));
app.use(cors());
app.get("/choices-api", function(req, res) {
res.json(choicesTerm);
});
这些是choices.js中的代码:
$(document).ready(function () {
$.getJSON('/choices-api', printTerms);
});
function printTerms(terms) {
var question = terms[0].question; //--> This is passed from app.js
var detail = terms[0].detail; //--> This is passed from app.js
}