简体   繁体   English

刷新Express NodeJS API的内容

[英]Refreshing contents of an Express NodeJS API

I have written an API that queries a MySQL database and outputs the corresponding results visiting an url. 我编写了一个查询MySQL数据库并输出访问URL的相应​​结果的API。 This is the code: 这是代码:

//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');

This script calls a required file where there are my queries: 该脚本在我的查询中调用所需文件:

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

exports.query1 = query1;

I go to the contents of this script this way: 我通过以下方式查看此脚本的内容:

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

When I first visit http://localhost:3000/AC149829.2_FGT004 , the API shows the correct results for the variable AC149829.2_FGT004 . 当我第一次访问http://localhost:3000/AC149829.2_FGT004 ,API会显示变量AC149829.2_FGT004的正确结果。 However, when changing the variable to AC148152.3_FGT007 , it continues showing the information for the variable AC149829.2_FGT004 . 但是,将变量更改为AC148152.3_FGT007 ,它将继续显示变量AC149829.2_FGT004的信息。 In order to see the results for AC148152.3_FGT007 , I must kill the script, call it again, and visit for the first time http://localhost:3000/AC148152.3_FGT007 . 为了查看AC148152.3_FGT007的结果,我必须AC148152.3_FGT007该脚本,再次调用它,并首次访问http://localhost:3000/AC148152.3_FGT007 In conclusion, results are not refreshed. 总之,结果没有刷新。

How is that? 那个怎么样? I tried with a simple: 我尝试了一个简单的方法:

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

but it works well... 但是效果很好...

EDIT . 编辑 I found the source of my problem. 我找到了问题的根源。 query1 is always the same. query1始终相同。 The script only calls once: 该脚本仅调用一次:

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

There's a way to overcome this limitation? 有办法克服这个限制吗?

I found the solution for my problem. 我找到了解决我问题的方法。 As

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

is executed once and remains in the cache, I changed my code without exporting var1 : 被执行一次并保留在缓存中,我更改了代码而未导出var1

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

and

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

exports.query1 = query1;

In other words, I import my query to server.js without any variable. 换句话说,我将查询导入到server.js没有任何变量。 The variable is assigned at server.js . 该变量在server.js分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM