gpt4 book ai didi

javascript - 刷新 Express NodeJS API 的内容

转载 作者:行者123 更新时间:2023-11-30 22:55:34 25 4
gpt4 key购买 nike

我写了一个 API,查询 MySQL 数据库并输出访问 url 的相应结果。这是代码:

//server.js
var express = require('express'),
mysql = require('mysql'),
app = express(),
connectionpool = mysql.createPool({
host : 'localhost',
user : 'root',
password : 'password',
database : 'database'
});

app.get('/:transcript', function(req,res){
var var1 = req.param('transcript');
exports.var1 = var1;
var queries = require('./queries'),
query1 = queries.query1;
//Connection to MySQL
connectionpool.getConnection(function(err, connection) {
if (err) {res.send({result: 'error connection'})}
connection.query(query1, function(err, rows) {
if (err) {res.send({result: 'error query1'})};
counter = 0; root = {};
rows.forEach(function (row) {
build_actor(row.Transcript_alias, function(exprobject1) {
counter += 1;
//Defining and filling objects
main = {};
main.Official_transcript_name = row.Transcript_name;
main.Expression = exprobject1;
root[row.Transcript_alias] = main;
if (counter == rows.length) {
res.write(JSON.stringify(root, null, '\t'));
res.end();
}
});
});
connection.release();
});
//CallBack
function build_actor(transcript, callback) {
//Other, secondary queries:
var query2 = 'SELECT * FROM expression WHERE transcript_alias = "' + transcript + '";',
connection.query(query2, function(err, rows1) {
if (err) {res.send({result: 'error query2'})}
var exprobject2 = {},
exprobject1 = {};
for (i = 0; i < rows1.length; i++) {
Conditions = rows1[i].conditions;
Tissue = rows1[i].tissue;
FPKM = rows1[i].FPKM;
exprobject2[Tissue] = FPKM;
if (Conditions in exprobject1) {
exprobject1[Conditions].push(exprobject2);
} else {
exprobject1[Conditions] = [];
exprobject1[Conditions].push(exprobject2);
}
}
callback(exprobject1);
});
}
});
});

app.listen(3000);
console.log('Listening on port 3000');

此脚本调用一个包含我的查询的必需文件:

//queries.js
var server = require('./server'),
query1 = 'SELECT distinct(transcript_alias)\
FROM transcript_features \
WHERE f.transcript_alias = "' + var1 + '";';

exports.query1 = query1;

我以这种方式转到此脚本的内容:

http://localhost:3000/AC149829.2_FGT004
http://localhost:3000/AC148152.3_FGT007

当我第一次访问 http://localhost:3000/AC149829.2_FGT004 时,API 显示变量 AC149829.2_FGT004 的正确结果。但是,当将变量更改为 AC148152.3_FGT007 时,它会继续显示变量 AC149829.2_FGT004 的信息。为了查看 AC148152.3_FGT007 的结果,我必须终止脚本,再次调用它,然后第一次访问 http://localhost:3000/AC148152.3_FGT007。总之,结果没有刷新。

怎么样?我尝试了一个简单的:

app.get('/:transcript', function(req,res){
var input = req.param('transcript');
res.send(input);
});

但是效果很好...

编辑。我找到了问题的根源。 query1 始终相同。该脚本只调用一次:

exports.var1 = var1;
var queries = require('./queries'),
query1 = queries.query1;

有办法克服这个限制吗?

最佳答案

我找到了解决问题的方法。作为

//server.js
exports.var1 = var1;
var queries = require('./queries'),
query1 = queries.query1;

执行一次并保留在缓存中,我更改了我的代码而没有导出 var1:

//server.js
var queries = require('./queries'),
query1 = queries.query1 + var1;

//queries.js
var server = require('./server'),
query1 = 'SELECT distinct(transcript_alias)\
FROM transcript_features \
WHERE f.transcript_alias = ';

exports.query1 = query1;

换句话说,我将我的查询导入到 server.js 而没有任何变量。该变量在 server.js 中分配。

关于javascript - 刷新 Express NodeJS API 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26536252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com