修改 我的问题稍有改动,所以我更新了我的代码并修改了我的问题。一切似乎都在起作用,除非我没有在浏览器中看到我的JSON。
这是我目前的代码:
'use strict';
var util = require('util');
var http = require('http');
var sql = require("mssql");
var express = require('express')
var port = process.env.PORT || 1337;
var membershipNumber;
var results;
var queryString;
var app = express();
function getParams() {
app.get('/:number', function (req, res) {
console.log("\nPARAMS:");
console.log(req.params.number);
membershipNumber = req.params.number;
queryString = util.format('SELECT major_key, status, paid_thru FROM name WHERE major_key = \'%s\' and member_record = 1', membershipNumber);
console.log("\nQUERY:");
console.log(queryString);
res.setHeader('Content-Type', 'application/json');
// res.end(queryDatabase(queryString));
res.send(queryDatabase(queryString));
});
app.listen(port);
}
function queryDatabase(query) {
var config = {
server: 'testserver',
database: 'test_db',
user: 'sa',
password: 'password',
port: 1433
};
var connection = new sql.Connection(config);
connection.connect().then(function () {
var req = new sql.Request(connection);
req.query(query).then(function (recordset) {
results = recordset;
console.log("\nRESULTS:");
console.log(results);
connection.close();
//Convert results to JSON string
var jsonString = JSON.stringify(results);
console.log("\njsonString:");
console.log(jsonString);
return jsonString;
})
.catch(function (err) {
console.log(err);
connection.close();
});
})
.catch(function (err) {
console.log(err);
});
}
getParams();
这就是我的console.log语句在控制台"
中打印的内容PARAMS:
0001354648
QUERY:
SELECT major_key, status, paid_thru FROM name WHERE major_key = '0001354648' and member_record = 1
RESULTS:
[ { major_key: '0001354648',
status: 'SN',
paid_thru: 2016-10-31T00:00:00.000Z } ]
jsonString:
[{"major_key":"0001354648","status":"SN","paid_thru":"2016-10-31T00:00:00.000Z"}]
答案 0 :(得分:0)
您有两个app.listen()
来电:一个在appFunction()
,另一个在getParams()
。您应该只调用app.listen()
一次并删除appFunction()
中的getParams()
,因为function queryDatabase(query) {
var config = {
server: 'dbserver',
database: 'test_db',
user: 'sa',
password: 'password',
port: 1433
};
var connection = new sql.Connection(config);
connection.connect().then(function() {
var req = new sql.Request(connection);
req.query(query).then(function(recordset) {
results = recordset;
console.log("\nRESULTS:");
console.log(results);
connection.close();
//Convert results to JSON string
jsonString = JSON.stringify(results);
console.log("\nJSON:");
console.log(jsonString);
appFunction(jsonString);
})
.catch(function(err) {
console.log(err);
connection.close();
});
})
.catch(function(err) {
console.log(err);
});
}
app.get('/:number', function(req, res) {
console.log("\nPARAMS:");
console.log(req.params.number);
membershipNumber = req.params.number;
queryString = util.format('SELECT major_key, status, paid_thru FROM name WHERE major_key = \'%s\' and member_record = 1', membershipNumber);
console.log("\nQUERY:");
console.log(queryString);
res.end(queryDatabase(queryString));
});
app.get('/', function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.send(jsonString);
});
app.listen(port);
似乎是您在初始化时调用的函数。
如果您确实需要以这种方式构建代码,请尝试以下方法:
queryDatabase()
请注意,这一次只能处理一个请求。
我强烈建议在进行REST调用时将GET http://<your-ip>:<your-port>/3
RESPONSE: { some JSON }
函数的输出返回给用户;在这种情况下,您的'/:number'路由将返回您想要接收的JSON字符串。在您的应用程序中,REST与此接口,您会看到以下内容:
// This function takes a file path and loads the data into a variable, then calls plot_data
function load_file(file) {
// removes previous SVG plots to prevent messy overlays and memory chaos
d3.selectAll("svg").remove();
// This code takes the file (which is a path, and puts it into var data, which is then given to the parcoords plot function)
d3.text(file, function(text) {
var data = d3.csv.parseRows(text).map(function(row) {
return row.map(function(value) {
return +value;
});
});
// This function plots the data given to it.
function plot_data(data) {
pc1 = d3.parcoords()("#example")
// This is how we get colour working. put a function in the parameters it iterates over each data item .color()
.data(data)
.render()
.alpha(0.35)
.brushMode("1D-axes")
.interactive() // command line mode
pc1.reorderable();
console.log("The plot should be done now")
};
此时你不需要'/'路线。