简体   繁体   中英

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. 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 . However, when changing the variable to AC148152.3_FGT007 , it continues showing the information for the variable 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 . 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. 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 :

//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. The variable is assigned at server.js .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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