Reputation: 151
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();
}
Upvotes: 0
Views: 75
Reputation: 944402
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.
Upvotes: 1