0

I want to configure an app that requires that I run node generate.js and node generate_texts_index.js on node's command prompt. These files are to build the data required for the app to work. When i run these files locally the app works in my browser. Now I have the same set of files located on my server, how can I run node generate.js when the files are on my server at www.example.com. I am new to node js. Thanks!

Here's what generate.js looks like

// MODULES
var fs = require('fs'),
    path = require('path'),
    bibleData = require('bible_data');


//console.log( bibleData.getBookInfoByUnboundCode('40N') );
//return;


// VARS
var
    baseOutput = '../../app/content/texts/',
    baseInput = 'input',
    createIndex = true;

console.log('\r\r\r');

function convertFolder(inputPath) {

    var infoFilePath = path.join(inputPath, 'info.json'),
        startDate = new Date();

    if (fs.existsSync(infoFilePath)) {

        var info = JSON.parse( fs.readFileSync(infoFilePath, 'utf8') ),
            generatorName = info.generator,
            generator = require('generate_' + generatorName),
            outputPath = path.join(baseOutput, info['id']),
            indexOutputPath = path.join(outputPath, 'index');

        console.log('-----');
        console.log(info['name'],  outputPath);

        // remove existing data
        if (fs.existsSync(outputPath)) {
            var files = fs.readdirSync(outputPath);

            // DELETE all files
            files.forEach(function(data) {
                var filePath = path.join(outputPath, data);
                if (fs.statSync(filePath).isFile()) {
                    fs.unlinkSync(filePath);                
                }
            });
        } else {
            fs.mkdirSync(outputPath);
        }

        // index data
        if (createIndex) {      
            if (fs.existsSync(indexOutputPath)) {
                var files = fs.readdirSync(indexOutputPath);

                // DELETE all files
                files.forEach(function(data) {
                    var filePath = path.join(indexOutputPath, data);
                    if (fs.statSync(filePath).isFile()) {
                        fs.unlinkSync(filePath);                
                    }
                });         
            } else {
                fs.mkdirSync(indexOutputPath);
            }           
        }

        generator.generate(inputPath, outputPath, indexOutputPath, info, createIndex);

        var endDate = new Date();       
        console.log('time: ' + MillisecondsToDuration(endDate - startDate));            
    }   
}

function convertFolders() {
    var files = fs.readdirSync(baseInput),
        startDate = new Date();

    // DO ALL
    for (var f in files) {
        var folder = files[f],
            inputPath = path.join(baseInput, folder);

        convertFolder(inputPath);
    }   

    var endDate = new Date();

    console.log('TOTAL: ' + MillisecondsToDuration(endDate - startDate));
}


function MillisecondsToDuration(n) {
    var hms = "";
    var dtm = new Date();
    dtm.setTime(n);

    var h = "000" + Math.floor(n / 3600000);
    var m = "0" + dtm.getMinutes();
    var s = "0" + dtm.getSeconds();
    var cs = "0" + Math.round(dtm.getMilliseconds() / 10);

    hms = h.substr(h.length-4) + ":" + m.substr(m.length-2) + ":";
    hms += s.substr(s.length-2) + "." + cs.substr(cs.length-2);

    return hms;
}


// START


// make /texts/ folder
if (!fs.existsSync(baseInput)) {
    fs.mkdirSync(baseInput);
}


// process 1 or more folders
if (process.argv.length > 2) {

    var folders = process.argv[2].split(',');

    folders.forEach(function(folder) {
        convertFolder(baseInput + '/' + folder);    
    });


} else {
    convertFolders();       
}
7
  • Could you please post the contents of generate.js. Commented Jan 14, 2014 at 11:04
  • I am not sure if I understood your question but...do you realize that javascript is a client-side scripting and have nothing to do with server? javascript via node.js is actually done via server but only establishes global functions/variables. But, to actually run javascript off the server as client-end is out of scope of the purpose. Commented Jan 14, 2014 at 11:06
  • I know that javascript is a client side language What I want to do is to serve the app online. Commented Jan 14, 2014 at 11:19
  • @dcodesmith I have posted the contents of generate.js. Please find it in the question above. Thanks. Commented Jan 14, 2014 at 11:31
  • @Faron — No. JavaScript is a programming language. It can run anywhere there is a suitable environment. Browsers provide one. Node provides one. Node is frequently used on servers so it has plenty to do with a server. Commented Jan 14, 2014 at 11:32

1 Answer 1

1

You need to run Node on your server.

Generally this would be done by using SSH to connect to the server and configuring it in the same way that you would for any other computer.

You won't be able to do this on low end hosting. You need to look at hosting that specifically advertises Node support, or a VPS or better.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Quentin what you're saying makes a lot of sense but I need you to break it down a little more cos I tried running node on my server but it just assumes I want to change the copy that already exists on my computer. So how do I run node?. There's SSH/shell access on the server but when i navigate there I get some stuff about security and all. I have never used any of this before so please treat me as s novice on this issue. However I need to get work done ASAP. Server is APACHE. Thanks again
You need to copy your JS to the server. Then log into the server with SSH and run a copy of node that is installed on the server. You probably want to write an init script so that it will start when the server boots and won't stop when you log out of ssh. If you want to involve Apache HTTPD, then you will need to look at mod_proxy so that requests for a given set of URLs will be forwarded to Node. You really need to have some knowledge of server / linux system administration, if you don't then a Stackoverflow answer isn't really the right place to get you up to speed. It's too broad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.