0

I have a node.js server pulling data using node-mysql and saving it into an array called FRUITS.

I need to transfer the value of this array (FRUITS) with the javascript file that i send to the client using 'readStream'..

Any ideas on how to do this? Code below

nodeController.js

function sendJSfile(req, res) {

    seed_id = "valid_fruits"

    var mysql = require('mysql');
    var end;
    var connection = mysql.createConnection({
        host: 'myhost',
        user: 'username',
        password: 'password',
        database: 'databasename'
    });
    connection.connect()
    connection.query({
        sql: 'SELECT fruit_type FROM fruit_table WHERE seed_id = ? LIMIT 0, 10',
        timeout: 4000,
        values: [seedid]
    }, function (err, result, fields) {

        var FRUITS = [];
        for (var i = 0; i < result.length; i++) {

            var obj = result[i]

            FRUITS.push(obj.fruit_type)
        }
    })

    res.writeHead(200, { "Content-Type": "text/javascript" });

    var readStream = fs.createReadStream("./fruits.js")
    readStream.on('open', function () {
        readStream.pipe(res);
    });
    readStream.on('error', function (err) {
        res.end(err);
    })

    Client.js

    // I use a post request because I need to send data to the server then receive the javascript file back via readStream

    request("myserver", "post", { apples })
        .done(function (res) {
        })
})


function request(url, method, data) {
    return $.ajax({
        url: url,
        method: method,
        data: data
    })
}
CoderPi
  • 12,985
  • 4
  • 34
  • 62

1 Answers1

0

This will append the fruits array as JSON to the js file and stream it:

connection.query({
    sql: 'SELECT fruit_type FROM fruit_table WHERE seed_id = ? LIMIT 0, 10',
    timeout: 4000,
    values: [seedid]
 }, function(err, result, fields) {

    var FRUITS = [];
    for(var i = 0; i < result.length; i++) {

        var obj = result[i]

        FRUITS.push(obj.fruit_type);
    }

    var fileVar = 'var FRUITS = '+JSON.stringify(FRUITS)+';';

    fs.appendFile('./fruits.js', fileVar, function(err) {
        res.writeHead(200, {
            "Content-Type": "text/javascript"
        });

        var readStream = fs.createReadStream("./fruits.js")
        readStream.on('open', function() {
            readStream.pipe(res);
        });
        readStream.on('error', function(err) {
            res.end(err);
        })
    });
 });

In this way you will append this var FRUITS = ["apple","banana"]; to the fruits.js so you can use FRUITS as variable if you use fruits.js as script.

michelem
  • 14,430
  • 5
  • 50
  • 66
  • Thanks - whats the best way to 'unpack' the appended FRUITS in the fruits.js? – PickMeUpLittle Dec 01 '15 at 13:53
  • having fruits.js as JSON too, so you can parse it, append the new array, write it and stream it. Otherwise you need to tell me what you do or what it contains fruits.js – michelem Dec 01 '15 at 13:54
  • I need to execute the javascript code in fruits.js, the code contained in this needs to iterate through the FRUITS array.. does that make sense? – PickMeUpLittle Dec 01 '15 at 14:01
  • yes so add it as variable I'm going to update the answer. – michelem Dec 01 '15 at 14:02
  • I've noticed its placing the array at the bottom of the js file, will there be a problem with sycronicity? I need the array to be inserted first and then the script in the js file to run? – PickMeUpLittle Dec 01 '15 at 14:24
  • `fs` can only append stuff to a file (or rewrite it completely) you cannot prepend. If you need to prepend you should use this module https://www.npmjs.com/package/prepend-file but I think the question is answered and you should write another one if you need more. – michelem Dec 01 '15 at 14:27