Writing data to JSON file using nodeJS takes too long - javascript

i am creating a function that Count the clicks of elements , and put them in JSON file
const fs = require('fs');
const file = fs.readFileSync('public/js/count.json');
const Words = JSON.parse(file);
const express = require('express');
const app = express();
app.listen(process.env.PORT || 3000, () => console.log('we are listeining'));
app.use(express.static('public'));
app.use(express.json({ limit : '1mb' }));
app.get('/add/:word', addWord);
function addWord(request, response) {
var data = request.params;
var word = data.word;
var reply;
var found = false;
for (i = 0; i < Words.length; i++){
if (Words[i].type == word){
Words[i].count++;
found = true;
break;
}
}
if (!found) {
Words.push({"type": word , "count": 1});
}
var x = JSON.stringify(Words, null, 2);
fs.writeFile('public/js/count.json', x, finished);
function finished(){
console.log('Yay')
}
/*
console.log(Words[word]); */
/* response.send(reply); */
}
when i run the code through my script
async function counter(elemid){
let response = await fetch("/add/"+elemid);
}
it takes too long to respond , and sometimes it gives request timeout , is there is a faster way to do the exact same purpose

You are not writing a response in your finished handler. This is leaving each request to only end via timeout.
In your finished function add response.end() at the end.
You can verify that this is working by ensuring that the request receives a 200 response from your server instead of timing out.

Related

request.body is giving me empty brackets or undefined

I am using body parsing middleware. I have installed the body-parser library. it seems as though I have tried everything but nothing is working.
here's my server code.
const express = require('express');
const app = express();
const bp = require('body-parser');
app.use(bp.json())
app.use(bp.urlencoded({ extended: true }))
app.use(express.static('/Users/anthonyquesen/Desktop/webzite/drive-download-20201123T191109Z-001/public'))
app.post('/api', async(req,res) => {
console.log(req.body.firstName);
res.json({status: 'ok'})
});
app.listen(3000, () =>{
console.log('server listening at 3000')
});
here's my client-side javascript code:
let charCreate = function(cCreator){
let lastName = "name"
let firstName= "reggy"
let cCreatorX = cCreator.windowWidth/1.6;
let charWindow;
let clickedCreator;
let bagClicked;
let dreamJournalClicked;
cCreator.setup = function()
{
cCreator.cCreatorCanvas = cCreator.createCanvas(cCreatorX-200,cCreatorX-200);
cCan = document.getElementById('charCreate');
cCreator.cCreatorCanvas.parent(cCan);
charWindow = characterCreate;
clickedCreator = false;
bagClicked = false;
dreamJournalClicked = false;
};
cCreator.draw = function()
{
cCreator.clear();
// cCreator.background(255,50)
cCreator.image(charWindow,0,-130,cCreatorX-200,cCreatorX);
if (cCreator.mouseX>cCreatorX/2-170 && cCreator.mouseX<cCreatorX/2 && cCreator.mouseY>cCreatorX/11-22 && cCreator.mouseY<cCreatorX/11-22+190 || bagClicked)
{
cCreator.image(seedBagOnHover,cCreatorX/2-170,cCreatorX/11-22,170,190);
}
else
{
cCreator.image(seedBag,cCreatorX/2-170,cCreatorX/11-22,170,190);
};
cCreator.image(dreamJournal,cCreatorX/2+10,cCreatorX/11+80,111,123);
};
cCreator.mouseClicked = function()
{
clickedCreator = true;
if (cCreator.mouseX>cCreatorX/2-170 && cCreator.mouseX<cCreatorX/2 && cCreator.mouseY>cCreatorX/11-22 && cCreator.mouseY<cCreatorX/11-22+190 && clickedCreator)
{
charWindow = gardenWreath;
bagClicked = true;
seedBagSound.play();
/** API request **/
fetch('/api', {
headers: {'Content-Type' : 'applications/json'},
method: "POST",
body: JSON.stringify({firstName,
lastName,
})
});
/** **/
console.log(JSON.stringify({firstName,lastName}));
};
};
if (cCreator.mouseX>cCreatorX/2+10 && cCreator.mouseX<cCreatorX/2+10+111 && cCreator.mouseY>cCreatorX/11+80 && cCreator.mouseY<cCreatorX/11+80+123)
{
charWindow = dreamerWreath;
dreamjournalClicked = true;
};
};
let cCreate = new p5(charCreate);
The entire function here is the name spaced/ in instance mode because I'm also using p5.js SDK If that has something to do with the code not working as well. there is a Lil more to the code of the client-side like a function that runs cCreator in p5 and other stuff but the main point is the fetch is not sending the code to the terminal. whenever I click the button it sends the post. It knows something sent it just comes back in the terminal as undefined. I'm hoping someone can take a look at this
thank you so much!

How can I overwrite and append data to the same file multiple times in node js

I have a "clients.txt" file where I have a list of emails. I try to run a program for sending emails where I chose a number of emails to use from the file, in that case the number is 2. After I use the two emails I want to overwrite "clients.txt" without them. The problem is when I try to run the code just for one single time every thing is working! but if I make a loop something is wrong. Looking forward to see any help from you guys. Thanks! I add the code bellow. PS: Sorry for my bad english!
function readEmails(){
const fs = require('fs');
clients_list = fs.readFileSync('clients.txt', 'utf8').split('\n');
let filtered = clients_list.filter(function (el) {
return el != null && el != '';
});
return filtered
}
function dump_array(arr, file){
let fs = require('fs');
let file = fs.createWriteStream(file);
file.on('error', function(err) { /* error handling */ });
arr.forEach(function(v) { file.write(v + '\n'); });
file.end();
}
while_var = 0;
while (while_var < 2){
while_var ++;
let all_clients = readEmails();
let selected_clients = [];
if (all_clients.length > 0){
selected_clients = all_clients.splice(0,2);
dump_array(all_clients, 'clients.txt');
console.log(selected_clients);
}else{
console.log('No more clients')
}
}
const fs = require('fs');
function readEmails(){
const clients_list = fs.readFileSync('clients.txt', 'utf8').split('\n');
const filtered = clients_list
// clear false, 0 and undefined too
.filter(el => !!el)
// remove extra spaces and \r symbols
.map(el => el.trim());
return filtered;
}
function dump_array(arr, file){
// Here you need sync method.
fs.writeFileSync(file, arr.join('\n'));
// And here was 'already declared' error in orginal code
}
let while_var = 0;
while (while_var++ < 2){
let all_clients = readEmails();
let selected_clients = [];
if (all_clients.length > 0){
selected_clients = all_clients.splice(0,2);
dump_array(all_clients, 'clients.txt');
console.log(selected_clients);
}else{
console.log('No more clients')
}
}

How to fix BeagleBone Black Service File Autostart Script Error, status=203/EXEC

I want my BeagleBone machine to automatically start a script called server.js.
It includes BoneScript (BeagleBone's unique function) and many functions including reading voltage and ampere from other machines.
I used this tutorial
Executing a script on startup using BeagleBone Black
It kept showing status=203/EXEC error.
I tried the same tutorial for simple programs without Bonescript and Functions.
Indeed, simple sending and receiving scripts are working without any issue.
How can I remove the error?
I think it happens from importing BeagleScript and Functions.
I'm not familiar with Linux environment.
Can anyone help to solve the issue?
Thanks,
https://stackoverflow.com/questions/28854705/executing-a-script-on-startup-using-beaglebone-black
-crontab
-service file
-8 days of trying, fail and searching Google
Service File (password hidden *****)
[Unit]
After=network-online.target
[Service]
Type=simple
ExecStart=/var/lib/cloud9/Projects echo ****** | sudo -S bash scriptname.sh
[Install]
WantedBy=multi-user.target
Shell File
#!/bin/bash
cd /var/lib/cloud9/Projects
node server.js
Javascript
// Constants and global parameters
const NUM_ADC_READS = 100;
const ADC_OFFSET = 0.027 // Offset for difference between ADC_GND and GND
const ZERO_OFFSET = 0.5; // Current sensor output centred at midpoint
const ADC_GAIN = 1.8; // Function returns 0-1, 0=0V, 1=1.8V
const VOLT_DIVIDER = 0.3491; // Voltage divider to reduce from 5V current sensor output to <1.8V ADC input
const MAX_VOLTAGE = 1.7456; // Max voltage divider output for max current sensor output
const CURRENT_CONVERSION = 25; // Current sensor sensitivity 25 A/V
const COMBINED_GAIN = 128.9; // CURRENT_CONVERSION * ADC_GAIN / VOLT_DIVIDER
const COMBINED_OFFSET = 0.473; // ZERO_OFFSET - ADC_OFFSET
//Loading modules
var http = require('http');
var fs = require('fs');
var path = require('path');
var b = require('bonescript');
// Create variables for relays
var relay_load_1 = "P8_12"; // Relay control for output load 1
var relay_load_2 = "P8_11"; // Relay control for output load 2
var relay_bat_bank_1 = "P8_7"; // Relay control for input battery bank 1
var relay_bat_bank_2 = "P8_8"; // Relay control for input battery bank 2
var relay_bat_bank_3 = "P8_10"; // Relay control for input battery bank 3
// Create variables for current readings
var current_load = ["P9_39", "P9_40"];
var current_bat = ["P9_37", "P9_38", "P9_33"];
// Initialize the relay control variables as OUTPUTS
b.pinMode(relay_load_1, b.OUTPUT);
b.pinMode(relay_load_2, b.OUTPUT);
b.pinMode(relay_bat_bank_1, b.OUTPUT);
b.pinMode(relay_bat_bank_2, b.OUTPUT);
b.pinMode(relay_bat_bank_3, b.OUTPUT);
// Initialize the server on port 8888
var server = http.createServer(function (req, res) {
// requesting files
var file = '.'+((req.url=='/')?'/index.html':req.url);
var fileExtension = path.extname(file);
var contentType = 'text/html';
// Uncoment if you want to add css to your web page
/*
if(fileExtension == '.css'){
contentType = 'text/css';
}*/
fs.exists(file, function(exists){
if(exists){
fs.readFile(file, function(error, content){
if(!error){
// Page found, write content
res.writeHead(200,{'content-type':contentType});
res.end(content);
}
})
}
else{
// Page not found
res.writeHead(404);
res.end('Page not found');
}
})
}).listen(1111);
// Loading socket io module
var io = require('socket.io').listen(server);
// When communication is established
io.on('connection', function (socket) {
socket.on('changeState_load_1', handleChangeState_load_1);
});
io.on('connection', function (socket) {
socket.on('changeState_load_2', handleChangeState_load_2);
});
io.on('connection', function (socket) {
socket.on('changeState_bat_1', handleChangeState_bat_1);
});
io.on('connection', function (socket) {
socket.on('changeState_bat_2', handleChangeState_bat_2);
});
io.on('connection', function (socket) {
socket.on('changeState_bat_3', handleChangeState_bat_3);
});
// Change relay state when a button is pressed
function handleChangeState_load_1(data) {
var newData = JSON.parse(data);
var status;
if (newData.state == 1) {
status = "OFF"
}
else {
status = "ON"
}
console.log("Load 1 Relay =" + status);
//console.log("Load 1 Relay =" + newData.state);
// turn the load relay ON or OFF
b.digitalWrite(relay_load_1, newData.state);
return status;
}
//function handleChangeState_load_1(data) {
// var newData = JSON.parse(data);
// console.log("Load 1 Relay =" + newData.state);
// turn the load relay ON or OFF
// b.digitalWrite(relay_load_2, newData.state);
// return newData.state;
//}
// Change relay state when a button is pressed
function handleChangeState_load_2(data) {
var newData = JSON.parse(data);
console.log("Load 2 Relay =" + newData.state);
// turn the load relay ON or OFF
b.digitalWrite(relay_load_2, newData.state);
}
// Change relay state when a button is pressed
function handleChangeState_bat_1(data) {
var newData = JSON.parse(data);
console.log("Bat 1 Relay =" + newData.state);
// turn the battery bank relay ON or OFF
b.digitalWrite(relay_bat_bank_1, newData.state);
}
// Change relay state when a button is pressed
function handleChangeState_bat_2(data) {
var newData = JSON.parse(data);
console.log("Bat 2 Relay =" + newData.state);
// turn the battery bank relay ON or OFF
b.digitalWrite(relay_bat_bank_2, newData.state);
}
// Change relay state when a button is pressed
function handleChangeState_bat_3(data) {
var newData = JSON.parse(data);
console.log("Bat 3 Relay =" + newData.state);
// turn the battery bank relay ON or OFF
b.digitalWrite(relay_bat_bank_3, newData.state);
}
// Read load currents from analog inputs
function readLoadCurrent(data) {
var current = 0;
var max_current = 0;
var min_current =1.0;
for (let i = 0; i < NUM_ADC_READS; i++) {
current = b.analogRead(current_load[data]);
if (max_current < current) max_current = current;
if (min_current > current) min_current = current;
}
// console.log("Load Current =" + ((max_current - min_current)/2.0));
return ((((max_current - min_current)/2.0)) * COMBINED_GAIN);
}
// Read battery bank currents from analog inputs
function readBatCurrent(data) {
var current = 0;
var max_current = 0;
var min_current = 1.0;
for (let i = 0; i < NUM_ADC_READS; i++) {
current = b.analogRead(current_bat[data]);
if (max_current < current) max_current = current;
if (min_current > current) min_current = current;
}
// console.log("Bat Current =" + ((max_current - min_current)/2.0));
return ((((max_current - min_current)/2.0)) * COMBINED_GAIN);
}
var listener = io.listen(server);
var currentBat_1 = readBatCurrent(1);
function battery1power(data){
while(1) { return currentBat_1*data;
}
}
listener.sockets.on('connection', function(socket){
//send data to client
setInterval(function(){
socket.emit('battery1power', {'battery1power': battery1power(0)});
}, 2000);
});
listener.sockets.on('connection', function(socket){
//send data to client
setInterval(function(){
socket.emit('currentLoad_1', {'currentLoad_1': readLoadCurrent(0)});
}, 2000);
});
listener.sockets.on('connection', function(socket){
//send data to client
setInterval(function(){
socket.emit('currentLoad_2', {'currentLoad_2': readLoadCurrent(1)});
}, 2000);
});
listener.sockets.on('connection', function(socket){
//send data to client
setInterval(function(){
socket.emit('currentBat_1', {'currentBat_1': readBatCurrent(0)});
}, 2000);
});
listener.sockets.on('connection', function(socket){
//send data to client
setInterval(function(){
socket.emit('currentBat_2', {'currentBat_2': readBatCurrent(1)});
}, 2000);
});
/*listener.sockets.on('connection', function(socket){
//send data to client
setInterval(function(){
socket.emit('currentBat_3', {'currentBat_3': readBatCurrent(2)});
}, 2000);
});*/
// Displaying a console message for user feedback
server.listen(console.log("Server Running ..."));
var currentBat_2 = readBatCurrent(1);
function battery2power(voltage){
while(1){ return currentBat_2*voltage;}}
var currentBat_3 = readBatCurrent(2);
function battery3power(voltage){
return currentBat_3*voltage;
}
var currentLoad_1 = readLoadCurrent(0);
function load1power(voltage){
return currentLoad_1*voltage;
}
var currentLoad_2 = readLoadCurrent(1);
function load2power(voltage){
return currentLoad_2*voltage;
}
var dgram = require('dgram');
var client = dgram.createSocket('udp4');
// Create periodical which ends a message to the client every 5 seconds
// 1000 ms is 1 second, so 5 second is 5000 ms
var interval = setInterval(function() {
//console.log('powerBat_1 ' + battery1power(116).toFixed(4));
client.send(
//'Serial Number: A33******************' + load1shandleChangeState_load_1(0) +
'33333powerBat_1 ' + battery1power(116).toFixed(4) +
';powerBat_2 ' + battery2power(110).toFixed(4) +
';powerBat_3 ' + battery3power(110).toFixed(4) +
';powerLoad_1 ' + load1power(116).toFixed(4) +
';powerLoad_2 ' + load2power(110).toFixed(4) +
';currentLoad_1 ' + readLoadCurrent(0).toFixed(4) +
';currentLoad_2 ' + readLoadCurrent(1).toFixed(4) +
';currentBat_1 ' + readBatCurrent(0).toFixed(4) +
';currentBat_2 ' + readBatCurrent(1).toFixed(4) +
';currentBat_3 ' + readBatCurrent(2).toFixed(4), 0, 10000, PORT, HOST, 0);
},5);
Error Message
● scriptname.service
Loaded: loaded (/etc/systemd/system/scriptname.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2019-09-05 23:47:06 UTC; 8s ago
Main PID: 27859 (code=exited, status=203/EXEC)
You may want to try to set up your .service file like this:
[Unit]
Description=Your Description for your File!
[Service]
ExecStart=/home/debian/var/lib/cloud9/file.js
[Install]
WantedBy=multi-user.target
Use the .service file outside of the Cloud9 IDE. So, this will help:
Create a directory
cd into that directory
Put your files in the directory you just created and cd'd into
Last but not least, use the /etc/systemd/system/ dir. to establish your .servive file.
Now...
If you are in your /home/debian/ dir. and have already created your .service file, change the .service file to look like this idea:
[Unit]
Description=Whatever you want goes here to describe your file use...
[Service]
ExecStart=/home/debian/<NewlyMadeDirectory>/<TheFileYouWantToRunWithFileSuffix>
[Install]
WantedBy=multi-user.target
Try that idea and get back to me.
Seth
P.S. I will wait to hear from you on how this worked out. Oh and you need, in the node.js file, to make sure it is executable and then use chmod a+x yourFile.js to set permissions.

How can I call a restapi function from frontend (ejs/html) [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
I am trying to basically call the 'get' method from html/ejs when a button is pressed to query the database and display the results. Sorry if it's very simple, but I just can't get it work.
I tried to require the file and call the function inside a script tag but that doesn't work on the browser. I tried to add the js file with a <script src="text/javascript" src="filename"> but that also results in errors.
The rest API I built talks to oracle 11g (Toad) using oracledb.
I am basically trying to call the get function in this class
const employees = require('../db_apis/employees.js');
async function get(req, res, next) {
try {
const context = {};
context.id = parseInt(req.params.id, 10);
const rows = await employees.find(context);
if (req.params.id) {
if (rows.length === 1) {
res.status(200).json(rows[0]);
} else {
res.status(404).end();
}
} else {
res.status(200).json(rows);
}
} catch (err) {
next(err);
}
}
...
db_apis/employees.js
const oracledb = require('oracledb');
const database = require('../services/database');
async function find(context) {
const baseQuery =
`SELECT *
FROM choice_names`;
console.log('in find');
let query = baseQuery;
const binds = {};
let additionalQuery = '\nwhere ';
if (context.id) {
binds.choice_name = context.id;
additionalQuery += `choice_name = :choice_name`;
// query += `\nwhere choice_name = :choice_name`;
} else if (context.user) {
binds.choice_user = context.user;
additionalQuery += `choice_user = :choice_user`;
} else if (context.date) {
binds.choice_date = context.date;
additionalQuery += `choice_date = :choice_date`;
}
if (context.id || context.user || context.date) {
query += additionalQuery;
}
console.log(query);
const result = await database.simpleExecute(query, binds);
return result.rows;
}
...
router.js
const express = require('express');
const router = new express.Router();
const employees = require('../controllers/employees');
router.route('/employees/:id?')
.get(employees.get)
.post(employees.post)
.put(employees.put)
.delete(employees.delete);
module.exports = router;
index.ejs
...
<button onclick="get()">Click me</button>
...
You are (very)confusing frontend and backend code.
The express app is in backend, running on some port, exposing the /employees/:id route.
But the frontend part doesn't have access to the backend scripts, So you need to do a XHR(Ajax) request from frontend to that route and get the result.
For example, in jQuery it can be something as
function get(){
$.get('/employees/1').done(..do something..)
}
You can refer this answer on how to do it on angular.

Node.js & Express: How to create many app.get calls with Express.js through "for loop"?

In my server.js, I am trying to loop through the array, that has different urls and use those urls for app.get request function.
Here is my code:
let articleUrlArray = [ 'https://techcrunch.com/2018/05/19/shared-housing-startups-are-taking-off/',
'https://techcrunch.com/2018/05/19/shared-housing-startups-are-taking-off/',
'https://techcrunch.com/2018/05/19/my-data-request-lists-guides-to-get-data-about-you/',
'https://techcrunch.com/2018/05/19/siempos-new-app-will-break-your-smartphone-addiction/',
'https://techcrunch.com/2018/05/19/la-belle-vie-wants-to-compete-with-amazon-prime-now-in-paris/',
'https://techcrunch.com/2018/05/19/apple-started-paying-15-billion-european-tax-fine/',
'https://techcrunch.com/2018/05/19/original-content-dear-white-people/',
'https://techcrunch.com/2018/05/19/meet-the-judges-for-the-tc-startup-battlefield-europe-at-vivatech/',
'https://techcrunch.com/2018/05/18/nasas-newest-planet-hunting-satellite-takes-a-stellar-first-test-image/',
'https://techcrunch.com/video-article/turning-your-toys-into-robots-with-circuit-cubes/',
'https://techcrunch.com/2018/05/18/does-googles-duplex-violate-two-party-consent-laws/' ];
for(var i = 0; i < articleUrlArray.length-1; i++) {
app.get('/news/news-desc', function(req, res) {
var data = '';
var techCrunchNewsItems = [];
request( articleUrlArray[i], function(err, response, html) {
var $ = cheerio.load(html);
if($('.article-content').children('p').eq(0).text().split(' ').length > 50) {
techCrunchNewsItems.push({
bodyOne: $('.article-content').children('p').eq(0).text()
});
} else {
techCrunchNewsItems.push({
bodyOne: $('.article-content').children('p').eq(0).text(),
bodyTwo: $('.article-content').children('p').eq(1).text()
});
}
data = techCrunchNewsItems;
res.send(JSON.stringify(data));
});
})
}
As you can see in my code, I have an array call "articleUrlArray" and created "for loop" to loop through this array to get each "articleUrl". Then use that "articleUrl" for request function and get the body content for that url.
No matter whatever happens, I always "only" get the body content for the last url. It is not getting the body content for every urls in the "articleUrlArray".
What am I doing wrong?
Here is the screenshot of what I am getting after running Hugo Nasciutti's solution below:
const articleUrlArray = [
'https://techcrunch.com/2018/05/19/shared-housing-startups-are-taking-off/',
'https://techcrunch.com/2018/05/19/shared-housing-startups-are-taking-off/',
'https://techcrunch.com/2018/05/19/my-data-request-lists-guides-to-get-data-about-you/',
'https://techcrunch.com/2018/05/19/siempos-new-app-will-break-your-smartphone-addiction/',
'https://techcrunch.com/2018/05/19/la-belle-vie-wants-to-compete-with-amazon-prime-now-in-paris/',
'https://techcrunch.com/2018/05/19/apple-started-paying-15-billion-european-tax-fine/',
'https://techcrunch.com/2018/05/19/original-content-dear-white-people/',
'https://techcrunch.com/2018/05/19/meet-the-judges-for-the-tc-startup-battlefield-europe-at-vivatech/',
'https://techcrunch.com/2018/05/18/nasas-newest-planet-hunting-satellite-takes-a-stellar-first-test-image/',
'https://techcrunch.com/video-article/turning-your-toys-into-robots-with-circuit-cubes/',
'https://techcrunch.com/2018/05/18/does-googles-duplex-violate-two-party-consent-laws/'
];
const checkBody = res => (err, response, html) => {
const $ = cheerio.load(html);
const articleContent = $('.article-content').children('p')
const bodyOne = articleContent.eq(0).text()
const bodyTwo = articleContent.eq(1).text()
const isExtensive = bodyOne.split(' ').length > 50
res(isExtensive ? { bodyOne } : { bodyOne, bodyTwo })
}
const getArticle = article => new Promise(res => request(article, checkBody(res)))
app.get('/news/news-desc', (req, res) => {
Promise.all(articleUrlArray.map(getArticle)).then(data => res.send(JSON.stringify(data)))
})
What is really going on here is that I am using a function to bring an array of Promises and when all of them are solved, then, respond the request with the array of objects stringified. I took the liberty of implementing arrow functions and constants.

Categories

Resources