Runnig javascript files located on my server via node js command prompt - javascript

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

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.

Related

Export/Require not working for Node.JS files

I have three JS files running under Node.JS.
- server.js - primary server file
- bidMgr.js - helper file
- hand.js - another helper file
They are back end server Express files. All are in the same directory.
hand.js exports a function named show:
exports.show = function(hand) {...}
server.js exports a function named announceBid:
exports.announceBid = function(newBid) {...}
bidMgr.js wants to call both of these functions.
So it requires each of the modules:
const handModule = require(__dirname + "/hand.js");
const serverModule = require(__dirname + "/server.js");
bidMgr.js calls the show function as shown:
handModule.show(players['North'].hand);
But when bidMgr.js tries to call the announceBid function as shown:
serverModule.announceBid(bidStr.charAt(0) + suit);
I get this error:
/home/Documents/FullStack/WebDevelopment/bid-server/bidMgr.js:212
serverModule.announceBid(nextBid.level + nextBid.suit);
TypeError: serverModule.announceBid is not a function
I can't see any difference in how these functions are being exported and required.
Yet one works and the other does not.
I've looked at dozens of posts on StackOverflow and tried all the suggested solutions, without success.
My only guess is that the server.js code also needs to call functions exported by bidMgr.js.
That is, the server.js code includes the command:
const bidMgr = require(__dirname + "/bidMgr.js");
Could the problem be a circular dependency?
Here are the snippets of code from each of the 3 files.
I have included in the snippets the require statements which are used,
each exported function from the file, and how that function is called in a different file.
In summary:
- server.js exports announceBid(), which is called in bidMgr.js
- bidMgr.js exports processHumanBid(), which is called in server.js
- hand.js exports the Hand() constructor, which is called in bidMgr.js
All use the export/require semantics.
The call of Hand() in NewGame() in bidMgr.js works.
The call of announceBid() in processHumanBid() in bidMgr.js results in a error.
From server.js
---------------
// jshint esversion:6
const bidMgr = require(__dirname + "/bidMgr.js");
const express = require("express", "4.17.1");
const app = express();
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
var connections = [],
history = [],
lastMessageId = 0,
uptimeTimeout = 10 * 1000,
totalRequests = 0;
function removeConnection(res) {
var i = connections.indexOf(res);
if (i !== -1) {
connections.splice(i, 1);
}
console.log("Removed connection index " + i);
}
function broadcast(event, message) {
message = JSON.stringify(message);
++lastMessageId;
history.push({
id: lastMessageId,
event: event,
message: message
});
connections.forEach(function (res) {
sendSSE(res, lastMessageId, event, message);
});
}
exports.announceBid = function(newBid) {
const bidStr = newBid.level.toString() + newBid.suit;
broadcast('bid', bidStr);
}
From bidMgr.js
---------------
// jshint esversion:6
// Require the card module
const card = require(__dirname + "/card.js");
// Require the deck module
const deck = require(__dirname + "/deck.js");
// Require the hand module
const handModule = require(__dirname + "/hand.js");
// Require the player module
const playerModule = require(__dirname + "/player.js");
const serverModule = require(__dirname + "/server.js");
function processHumanBid(bidStr) {
const level = Number(bidStr.charAt(0));
const suit = bidStr.charAt(1);
nextBid = {suit: suit, level: level};
console.log("Human bid " + nextBid.level + nextBid.suit + " for " + bidder);
serverModule.announceBid(bidStr.charAt(0) + suit);
}
function newGame() {
if (!allPlayersJoined) {
console.log("Cannot start a game without all the players");
return;
}
// Rotate the Dealer
dealer = playerModule.getNextPlayer(dealer);
console.log("Dealer is " + dealer);
bidder = dealer;
// Deal the cards
var dealtHands = [];
var bridgeHands = [];
// Get the dealer to pass out all the cards into 4 piles
dealtHands = deck.dealDeck();
// Oh yeah, we are playing bridge. Create 4 bridge hands using these piles
for (let i = 0; i < 4; i++) {
bridgeHands[i] = new handModule.Hand(dealtHands[i]);
};
}
From hand.js
------------
//jshint esversion:6
// Require the card module
const suitModule = require(__dirname + "/suit.js");
// Require the card module
const card = require(__dirname + "/card.js");
exports.Hand = function(dealtCards) {
this.handCards = [...dealtCards];
this.handCards.sort(function(a, b) {
if (a.index < b.index) {
return -1;
}
if (a.index > b.index) {
return 1;
}
// a must be equal to b
return 0;
});
this.hcPts = calcHCP(dealtCards);
calcDistribution(this, dealtCards);
this.totalPts = calcTotalPts(this);
this.openingBid = calcOpenBid(this);
this.player = null;
};
I suggest that you don't use from call a function in the server.js (announceBid). for send response to client you can use a file this like:
apiResponse.js :
'use sctrict';
var HttpStatus = require('http-status-codes');
exports.sendSucces = function (res, data) {
try {
if (res) {
if(data)
res.status(HttpStatus.OK).json(data);
else
res.status(HttpStatus.OK).send();
}
} catch (error) {
//log error
}
}

Can't process multiple pdf files using pdf2json for nodejs

I'm using https://github.com/modesty/pdf2json to parse multiple pdf files. It works with 1 single file, but when trying to load multiple files, the pdfParser_dataReadyevent seems to fire always with the same file.
This is what i've tried
var PDFParser = require('pdf2json');
var pdfParser = new PDFParser();
var fs = require('fs');
var fileNames = [];
var fileCont = 0;
fs.readdir(fileFolder, function(err, files){
for (var i = files.length - 1; i >= 0; i--) {
if (files[i].indexOf('.pdf') !== -1){
fileNames.push(files[i]);
}
pdfParser.loadPDF(fileNames[fileCont]);
});
pdfParser.on('pdfParser_dataReady', function(data){
//Do all my stuff and insert in db...
fileCont++;
If (fileCont === fileNames.lenght){
for (var i = fileNames.length - 1; i >= 0; i--) {
fs.unlink(fileFolder + fileNames[i]);
}
return res.json({
data: 'ok '
});
}
pdfParser.loadPDF(fileFolder + fileNames[fileCont]);
});
I managed to make pdf2json work with multiple files by creating a new PDFparser in each iteration. This is not a very 'pretty' way to manage multiple pdf files, the library should have an easy way of doing it, but it works!
var PDFParser = require('pdf2json');
var fs = require('fs');
var fileNames = [];
var fileFolder = 'myFolder/';
var fileCont = 0;
var loadPDF = function(filePath){
if(fileNames.length === fileCont){
//Insert in db and add any FINAL code, then return;
}
else{
//Call for another file to process
var pdfParser = null;
pdfParser = new PDFParser();
pdfParser.loadPDF(filePath);
pdfParser.on('pdfParser_dataError', function(err){
//Handle pdfParser error
});
pdfParser.on('pdfParser_dataReady', function(data){
//Get the pdf data and process it
fileCont++; //increase the file counter
loadPDF(fileFolder + fileNames[fileCont]); //parse the next file
});
}
};
fs.readdir(fileFolder, function(err, files){
for (var i = files.length - 1; i >= 0; i--) {
if (files[i].indexOf('.pdf') !== -1){
fileNames.push(files[i]);
}
}
loadPDF(fileFolder + fileNames[fileCont]);
});

Node.JS - want to move output off console into log/err files

Can someone give me a hand with converting the following code from console output to file output? I'm struggling with logging and the asynchronous nature of Node. The script works great in a console, but I'd like to pipe the sorted output into individual server sections within a file with STDERR going to another file.
var rexec = require('remote-exec');
var fs = require('fs');
var lineReader = require('line-reader');
var streamBuffers = require('stream-buffers');
var _ = require('lodash');
var conn_options = {
port: 22,
username: '*****',
privateKey: fs.readFileSync('R:/nodeJS/sshkey.priv')
}
// something that dumps out a bunch of data...
var cmds = ['df']
var filename = 'servers.txt';
lineReader.eachLine(filename,function(line,last,cb){
var buffer = new streamBuffers.WritableStreamBuffer();
var my_conn_options = _.clone(conn_options);
rexec(line,cmds,my_conn_options,function(err){
if (err) {
console.log(line, err);
} else {
console.log('>>>> Start: ' + line + '<<<<')
console.log(buffer.getContentsAsString());
console.log('>>>> End: ' + line + '<<<<')
};
});
if (last) {
cb(false); // stop reading
} else {
cb();
}
});
check this example, that should help..
var fs = require('fs');
var util = require('util');
var logFile = fs.createWriteStream('log.txt', { flags: 'a' });
// Or 'w' to truncate the file every time the process starts.
var logStdout = process.stdout;
console.log = function () {
logFile.write(util.format.apply(null, arguments) + '\n');
logStdout.write(util.format.apply(null, arguments) + '\n');
}
console.error = console.log;

node.js + serial code not work consistently

i tried to calculate root mean square using node.js, trying to be smart to make some kind of high resolution realtime measurement of stuff from serial port.
the code doesnt really matter. but each time i try to run this multiple times but it output totally different rms output value, sometimes very big, sometimes very small, NaN, etc.
the question is:
why? how? is there any node.js quirk i miss? like inter-process shared memory allocation?
it's fixed after i rewrite the program(some edit) or waiting for some time
re running a program should do the same thing everytime, right?
btw i'm using nodeserial for serial communication, at 500k baud
actual code may be posted later if you want
edit: the code
var math = require('mathjs');
var fs = require('fs');
// serving the static html "client"
var http = require('http');
var finalhandler = require('finalhandler');
var serveStatic = require('serve-static');
var serve = serveStatic("./");
var server = http.createServer(function(req, res) {
var done = finalhandler(req, res);
serve(req, res, done);
});
// socket.io
var app = require('express')();
var http2 = require('http').Server(app);
var io = require('socket.io')(http2);
io.on('connection', function(socket) {
console.log('client connected');
});
/* CONSOLE INTERFACE, USED TO MANUALLY INSERT SOME KIND OF VARIABLES*/
//stdin interaction
var stdin = process.openStdin();
var command = '';
var commandmode = false;
var bufferdata;
var command = 0;
//serial stuff
var serialport = require('serialport');
var SerialPort = serialport.SerialPort;
//portName = process.argv[2];
portName = "COM4"
//parsing commandline argument as comport name
var myPort = new SerialPort(portName, {
baudRate: 500000, // choose between: 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1800, 1200, 600, 300, 200, 150, 134, 110, 75, or 50
// look for return and newline at the end of each data packet:
parser: serialport.parsers.readline("\n")
});
myPort.on('open', showPortOpen);
myPort.on('data', sendSerialData);
myPort.on('close', showPortClose);
myPort.on('error', showError);
var datacount = 0;
function showPortOpen() {
console.log('port open. Data rate: ' + myPort.options.baudRate);
}
var timestamp;
var timestampold;
var dt;
function pad(num, size) {
var s = "0000" + num;
return s.substring(s.length - size);
}
function padd(num, size) {
var s = "00000000" + num;
return s.substring(s.length - size);
}
var vrms = [];
var irms = [];
var torq = [];
var speed = [];
var vraw = [];
var iraw = [];
function sendSerialData(data) {
if (datacount === 0) {
timestampold = Date.now();
}
if (datacount === 10) {
console.log(data);
}
if (datacount === 1000) {
timestamp = Date.now();
dt = timestamp - timestampold;
var samplerate;
var datalength = data.length + 1;
samplerate = datacount * 1000 / dt;
var troughput = datalength * samplerate;
var troughputp = 800 * troughput / 500000;
console.log(datacount + " data has been recorded. time elapsed:" + dt + ", samplerate:" + samplerate + " troughput:" + troughput + "/" + troughputp + "%");
}
var parseddata = data.split(",");
vraw.push(parseInt(parseddata[0], 16));
iraw.push(parseInt(parseddata[1], 16));
torq.push(parseInt(parseddata[2], 16));
speed.push(parseInt(parseddata[3], 16));
rmsfast(vraw[datacount], iraw[datacount]);
//var rawlog = padd(datacount,8) + "," + pad(vraw[datacount],4) + ","+ pad(iraw[datacount],4) +","+ pad(speed[datacount],4)+","+ pad(torq[datacount],4);
//var rawlog = padd(datacount, 8) + "," + vraw[datacount] + "," + iraw[datacount] + "," + speed[datacount] + "," + torq[datacount] + ">>" + vrms[datacount] + "," + irms[datacount];
//fs.appendFileSync('raw.txt', rawlog);
//console.log(rawlog);
++datacount;
}
function showPortClose() {
console.log('port closed.');
}
function showError(error) {
console.log('Serial port error: ' + error);
}
var varray = [0];
// zero setup;
var mid = 2048; //ideal; changed on zerocross detection
/* the data from microcontroller is as such: VVV,III,TTT,NNN
CC = COMMAND/RUNNING MODE
VVVV = VOLTAGE READING
IIII = CURRENT READING
TTTT = TORQUE READING
NNNN = SPEED (RPM) READING
Z = IS ZERO(0=false,1=voltage,2=current)
delta t between data (microsecond);
*/
var bufferV = [0];
var bufferI = [0];
var datacount;
var dataindex; // index of data called on the below stuff
var rmslength = 80;
var sigmaVsquared = 0;
var sigmaIsquared = 0;
var lastVvalue = 0;
var lastIvalue = 0;
var Vrms = 0;
var avgV = 0;
var avgI = 0;
function rmsfast(newVoltage, newCurrent) { // example use = rmsfast(data,buffer,)
var newSqVoltage = Math.pow(newVoltage, 2);
var newSqCurrent = Math.pow(newCurrent, 2);
bufferV.push(newSqVoltage);
if (bufferV.length === 80) {
lastVvalue = bufferV.shift();
} else {
//lastVvalue = newSqVoltage;
}
avgV += ((newSqVoltage - lastVvalue) / 80);
Vrms = math.sqrt(avgV);
bufferI.push(newSqCurrent);
lastIvalue = bufferI.shift();
avgI += (newSqCurrent - lastIvalue) / 80;
var Irms = math.sqrt(avgI);
var darn = math.sqrt(4000000);
if (datacount < 1000) {
var darz = Number.isInteger(newSqVoltage);
var vavg = avgV;
var vrmss = vrms;
console.log(newVoltage + "\t---" + avgV + "\t---" + Vrms);
}
if (datacount === 1000) {
myPort.close(process.exit());
}
vrms.push(Vrms);
irms.push(Irms);
}
server.listen(2000); // listening to the port
stdin.addListener("data", function(cons) { //"listening" to stdin
command = cons.toString().trim();
});

How to write to CSV file in Javascript

I have a script (using PhantomJS) that tests how long it takes to load a webpage. What I am trying to figure out is how to write the result of time taken to load the page to a .csv file. Then if I were to re-run the test again for it to add another result to the .csv file.
code:
var page = require('webpage').create(),
system = require('system'),
t, address;
var pageLoadArray = [];
var csvContents = "";
fs = require('fs');
if (system.args.length === 1) {
console.log('Usage: loadspeed.js <some URL>');
phantom.exit(1);
} else {
t = Date.now();
address = system.args[1];
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
}
else {
t = Date.now() - t;
console.log('Page title is ' + page.evaluate(function () {
return document.title;
}));
if(t>7000){
console.log('Loading time was too long... ' + t + "msec");
pageLoadArray.push(t);
console.log(pageLoadArray.length);
console.log(pageLoadArray[0]);
//store the time value to the .csv file
phantom.exit(1);
}
else{
console.log('Loading time ' + t + ' msec');
pageLoadArray.push(t);
console.log(pageLoadArray.length);
console.log(pageLoadArray[0]);
//store the time value to the .csv file
}
}
phantom.exit();
});
}
You can use the fs module with the write(path, content, mode) method in append mode.
var fs = require('fs');
fs.write(filepath, content, 'a');
where filepath is the file path as a string and content is a string containing your CSV line.
Something like:
address+";"+(new Date()).getTime()+";"+t
If you have control over the Jenkins environment, you can use one of the browser specific methods of triggering a download like suggested in This Question
function download(strData, strFileName, strMimeType) {
var D = document,
A = arguments,
a = D.createElement("a"),
d = A[0],
n = A[1],
t = A[2] || "text/plain";
//build download link:
a.href = "data:" + strMimeType + "charset=utf-8," + escape(strData);
if (window.MSBlobBuilder) { // IE10
var bb = new MSBlobBuilder();
bb.append(strData);
return navigator.msSaveBlob(bb, strFileName);
} /* end if(window.MSBlobBuilder) */
if ('download' in a) { //FF20, CH19
a.setAttribute("download", n);
a.innerHTML = "downloading...";
D.body.appendChild(a);
setTimeout(function() {
var e = D.createEvent("MouseEvents");
e.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.dispatchEvent(e);
D.body.removeChild(a);
}, 66);
return true;
}; /* end if('download' in a) */
//do iframe dataURL download: (older W3)
var f = D.createElement("iframe");
D.body.appendChild(f);
f.src = "data:" + (A[2] ? A[2] : "application/octet-stream") + (window.btoa ? ";base64" : "") + "," + (window.btoa ? window.btoa : escape)(strData);
setTimeout(function() {
D.body.removeChild(f);
}, 333);
return true;
}
Maybe you can use this URL SCM Plugin to grab the download.
Or use Selenium to automate some things and grab the download file

Categories

Resources