通过node js命令提示符运行位于我服务器上的javascript文件

时间:2014-01-14 11:03:07

标签: javascript node.js

我想配置一个应用,要求我在节点的命令提示符下运行node generate.jsnode generate_texts_index.js。这些文件用于构建应用程序运行所需的数据。当我在本地运行这些文件时,应用程序在我的浏览器中工作现在我的服务器上有相同的文件集,当文件在我的服务器 www.example.com 时,如何运行node generate.js。我是节点js的新手。谢谢!

这是generate.js的样子

// 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();       
}

1 个答案:

答案 0 :(得分:1)

您需要在服务器上运行Node。

通常,这可以通过使用SSH连接到服务器并以与您对任何其他计算机相同的方式进行配置来完成。

您无法在低端托管上执行此操作。您需要查看专门宣传Node支持或VPS或更好的托管。