NodeJS rmdir function error on LINUX ONLY? - javascript

So I'm running this section of my NodeJS script on my Windows 10 device running the latest version of Node and another version on my webserver running CentOS8 Linux. When this code is run on windows, it outputs what I would expect but when I run it on my Linux machine, it errors on this line:
fs.rmdir('public/u/' + info[0] + '/', { recursive: true }, (err) => {
With this error:
fs.js:136
throw new ERR_INVALID_CALLBACK();
^
TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
at makeCallback (fs.js:136:11)
at Object.rmdir (fs.js:671:14)
at /home/frontlinemist57server/celeste.js:156:8
at /home/frontlinemist57server/node_modules/line-reader/lib/line_reader.js:279:15
at getLine (/home/frontlinemist57server/node_modules/line-reader/lib/line_reader.js:166:7)
at Object.nextLine (/home/frontlinemist57server/node_modules/line-reader/lib/line_reader.js:183:7)
at Immediate.readNext (/home/frontlinemist57server/node_modules/line-reader/lib/line_reader.js:269:14)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
Any ideas?
app.post('/api/purge', (req, res) => {
// Get Sent Token //
var giventoken = req.body.token;
var authed = false;
lineReader.eachLine('accounts.txt', function(line, last) {
// If Valid Token //
if (crypto.createHash('md5').update(line).digest("hex") == giventoken) {
authed = true;
var info = line.split("§");
if (fs.existsSync('public/u/' + info[0] + '/')){
fs.rmdir('public/u/' + info[0] + '/', { recursive: true }, (err) => {
if (err) {
res.send(err);
throw err;
} else {
console.log("Account Purged: " + info[0]);
res.send('purged');
}
}
)}
// If Invalid Token //
} else if (last) {
if (authed == false) {
console.log("Invalid Purge Token: " + giventoken);
res.write("invalid");
res.end();
}
}
});
});
Edit: Both Windows and Linux systems are running the same version of node.

It turns out that DNF doesn't actually update you to the latest version of node for some reason. The server was still running an out of date version of node.
This is how I fixed it:
sudo dnf rm nodejs
curl -sL https://rpm.nodesource.com/setup_12.x | sudo bash -
sudo dnf install nodejs

Related

I want to first connect to aws and hit a python command from Nodejs via Api. Not sure how to do?

I want to execute 'python3 sample.py --path.data=/root/tej4' command from nodejs.The below is my code -
const runningPython = async(req,res,next) => {
ssh.connect({
host: '******',
port: 22,
username: 'ubuntu',
privateKey: '*******'
}).then(function () {
exec('python3 sample.py --path.data=/root/tej4', (err, stdout, stderr) => {
if (err) {
console.log(err)
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
})
}
But what I am getting response is its trying to run locally rather than on aws cli. Not sure how to achieve this?
Error: Command failed: python3 sample.py --path.data=/root/tej4
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
at ChildProcess.exithandler (node:child_process:397:12)
at ChildProcess.emit (node:events:390:28)
at maybeClose (node:internal/child_process:1064:16)
at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5) {
killed: false,
code: 9009,
signal: null,
cmd: 'python3 sample.py --path.data=/root/tej4'
}
stdout:
stderr: Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
You have to create an instance of type node_ssh and use that instance to call connect and executeCommand methods
const node_ssh = require('node-ssh');
const ssh = new node_ssh();
ssh
.connect({
host: '******',
port: 22,
username: 'ubuntu',
privateKey: '*******'
})
.then(() => {
ssh.execCommand('python3 sample.py --path.data=/root/tej4', { cwd:'/your-python-script-path-here' })
.then((result) => {
console.log('STDOUT: ' + result.stdout)
console.log('STDERR: ' + result.stderr)
});
You can use ssh lib, but for sudo su and running multiple commands switch to ssh2shell, the code for that is mentioned below:
const runningPython = async (req, res, next) => {
var host = {
server: {
host: ***** ,
username: ***** ,
privateKey: fs.readFileSync('ppk file path'),
},
commands: ["sudo su -", "python3 sample.py --path.data=root/tej4"],
onCommandComplete: function (command, response, sshObj) {
if (sshObj.debug) {
this.emit("msg", this.sshObj.server.host + ": host.onCommandComplete event, command: " + command);
}
if (command === "sudo su -" && response.indexOf("/root") != -1) {
sshObj.commands.unshift("msg:The command and response check worked. Added another cd command.");
sshObj.commands.unshift("cd .ssh");
} else if (command === "python3 sample.py --path.data=root/tej4") {
this.emit("msg", response);
}
},
onEnd: function (sessionText, sshObj) {
if (sshObj.debug) {
this.emit("msg", this.sshObj.server.host + ": host.onEnd event");
}
}
};
let SSH = new SSH2Shell(host),
//Use a callback function to process the full session text
callback = function (sessionText) {
console.log(sessionText)
}
//Start the process
SSH.connect(callback);
}

Why my RestFul API with express and nodejs crashed everyday?

I have restful api with express and nodejs but this api crashed every time.
So.. I have one function for datatime. This function will replace date in the url address everytime to current datetime.. Im not sure but may be when the current date is change with the new current date the API crashed..
I see this error message on the console:
events.js:187
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:201:27)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError (C:\Users\Admin\Desktop\node-express\node_modules\mysql\lib\Connection.js:426:8)
at Protocol.emit (events.js:210:5)
at Protocol._delegateError (C:\Users\Admin\Desktop\node-express\node_modules\mysql\lib\protocol\Protocol.js:398:10)
at Protocol.handleNetworkError (C:\Users\Admin\Desktop\node-express\node_modules\mysql\lib\protocol\Protocol.js:371:10)
at Connection._handleNetworkError (C:\Users\Admin\Desktop\node-express\node_modules\mysql\lib\Connection.js:421:18)
at Socket.emit (events.js:210:5)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: 'ECONNRESET',
code: 'ECONNRESET',
syscall: 'read',
fatal: true
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! express-api#1.0.0 start: `node server.js`
npm ERR!
npm ERR! Failed at the express-api#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Admin\AppData\Roaming\npm-cache\_logs\2019-12-29T04_48_17_190Z-debug.log
So this is the code for api.. Its very simple.. but I dont know how to fix this error.
When I wake up and try to see the data from the API every time the API is crashed.
This is the code from the api:
// Create express app
var express = require("express")
var app = express()
var mysql = require('mysql')
var express = require("express")
var cors = require('cors')
app.use(cors())
// Server port
var HTTP_PORT = 8000
// Start server
app.listen(HTTP_PORT, () => {
console.log("Server running on port %PORT%".replace("%PORT%", HTTP_PORT))
});
var con = mysql.createConnection({
host: "192.168.0.1",
port: "1234",
user: "username",
password: "password"
});
let aladinModel = '';
let aladinModelStations = '';
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
var dateNow = formatDate(Date());
app.route('/')
.get(function (req, res) {
// omitted
res.setHeader('Access-Control-Allow-Origin', '*', 'Cache-Control', 'private, no-cache, no-store, must-revalidate');
const date = req.query.date;
const id = req.query.id;
const daysForward = req.query.daysForward;
try {
const query = `CALL aladin_surfex.Get_mod_cell_values_meteogram_cell('${dateNow}', ${id}, ${daysForward})`;
con.query(query, function (err, result, fields) {
if (err) throw err;
aladinModel = result;
});
res.json({ aladinModel })
} catch (error) {
console.log("Error query database!!!");
}
});
app.route('/stations')
.get(function (req, res) {
// omitted
res.setHeader('Access-Control-Allow-Origin', '*');
try {
const query2 = `SELECT Station,Ime FROM aladin_surfex.stations_cells;`;
con.query(query2, function (err, result2, fields) {
if (err) throw err;
aladinModelStations = result2;
});
res.json({ aladinModelStations })
} catch (error) {
console.log("Error query database!!!");
}
});
app.use(function (req, res) {
res.status(404);
});
I try to remove the cashe, to update npm, to restart the computer. But without the result.
With nodemon crashed the same..
What can I do ? How can to fix that ?
The error seems to be related to the connection to mysql.
As mysqljs documentation (https://github.com/mysqljs/mysql#error-handling):
Note: 'error' events are special in node. If they occur without an attached listener, a stack trace is printed and your process is killed.
You shuld intercept the connection error like this:
connection.on('error', function(err) {
console.log(err.code); // 'ER_BAD_DB_ERROR'
});
so you can investigate when and why the error occours and eventually you can recreate the connection when a problem occurs.
Move app.listen to the bottom of the file, after you declare all the routes and connect to the database.
Use app.get('route', function...) (more on that in the Express docs)
Move res.json() inside the callback function for each database query. The result will come back asynchronously so will not be accessible outside the function. If you’re new to async and callbacks in Javascript I’d recommend googling them and you’ll find a ton of reading materials.
Initialize your variables, e.g const aladinModel = ...

NodeJs, if statements inside connection.query

i am a c# guy trying to create a login page using in react, node and mysql. i have a doubt that can i write 'if statements inside connection.query'. Below is the code snippet i am using. When i am trying to run this script, i am getting proxy error.
app.post('/api/world', (req, res) => {
try{
console.log(req.body);
uname = req.body.post;
var DBuserCheck = "SELECT EXISTS (SELECT * FROM `loginschema`.credentials WHERE uname =" + '"' + uname + '"' + ")";
var DBpwdCheck = "SELECT pwd FROM `loginschema`.credentials WHERE uname =" + '"' + uname + '"';
connection.query(DBuserCheck, function(err,data){
if(DBuserCheck){
connection.query(DBpwdCheck, function(err, pdata){
if(pdata == req.body.password){
res.json({Successfully_loggedin});
}
(err)? res.send(err) : res.json({pwdData_Wrong_Password: pdata});
console.log('enter valid credentials');
});
}
(err) ? res.send(err) : res.json({ USERdata: data });
console.log('user query data: dataSTR',+data +" ; req string "+ req.body.post +"; uname string "+ uname);
});
}
catch(ex){
console.log('Message from Catch is ----->>>> '+ex);
}
});
Is my code correct or i have to follow any other procedure. My code first runs correctly and crashes at the ending. I am getting the below error.
PS H:\PyProjects\login\loginproj> yarn dev
yarn run v1.17.3
$ concurrently --kill-others-on-fail "yarn server" "yarn client"
$ cd client && yarn start
$ nodemon server.js
$ react-scripts start
[0] [nodemon] 1.19.4
[0] [nodemon] to restart at any time, enter `rs`
[0] [nodemon] watching dir(s): *.*
[0] [nodemon] watching extensions: js,mjs,json
[0] [nodemon] starting `node server.js`
[0] Listening on port 5000
[1] Browserslist: caniuse-lite is outdated. Please run next command `yarn upgrade caniuse-lite browserslist`
[1] Starting the development server...
[1]
[1] Browserslist: caniuse-lite is outdated. Please run next command `yarn upgrade`
[1] Browserslist: caniuse-lite is outdated. Please run next command `yarn upgrade`
[1] Compiled successfully!
[1]
[1] You can now view create-react-app-express in the browser.
[1]
[1] Local: http://localhost:3000/
[1] On Your Network: http://10.0.0.65:3000/
[1]
[1] Note that the development build is not optimized.
[1] To create a production build, use yarn build.
[1]
[0] {}
[0] user query data: dataSTR NaN ; req string undefined; uname string undefined
[0] H:\PyProjects\login\loginproj\node_modules\mysql\lib\protocol\Parser.js:437
[0] throw err; // Rethrow non-MySQL errors
[0] ^
[0]
[0] Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[0] at ServerResponse.setHeader (_http_outgoing.js:470:11)
[0] at ServerResponse.header (H:\PyProjects\login\loginproj\node_modules\express\lib\response.js:771:10)
[0] at ServerResponse.send (H:\PyProjects\login\loginproj\node_modules\express\lib\response.js:170:12)
[0] at ServerResponse.json (H:\PyProjects\login\loginproj\node_modules\express\lib\response.js:267:15)
[0] at Query.<anonymous> (H:\PyProjects\login\loginproj\server.js:44:36)
[0] at Query.<anonymous> (H:\PyProjects\login\loginproj\node_modules\mysql\lib\Connection.js:525:10)
[0] at Query._callback (H:\PyProjects\login\loginproj\node_modules\mysql\lib\Connection.js:491:16)
at Query.Sequence.end (H:\PyProjects\login\loginproj\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
[0] at Query._handleFinalResultPacket (H:\PyProjects\login\loginproj\node_modules\mysql\lib\protocol\sequences\Query.js:139:8)
[0] at Query.EofPacket (H:\PyProjects\login\loginproj\node_modules\mysql\lib\protocol\sequences\Query.js:123:8)
[0] [nodemon] app crashed - waiting for file changes before starting...
[1] Proxy error: Could not proxy request /api/world from localhost:3000 to http://localhost:5000/.
[1] See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNREFUSED).
Accoding to your error from your code send response multiple times, so here is your working code
app.post('/api/world', (req, res) => {
try {
console.log(req.body);
uname = req.body.post;
var DBuserCheck = "SELECT EXISTS (SELECT * FROM `loginschema`.credentials WHERE uname =" + '"' + uname + '"' + ")";
var DBpwdCheck = "SELECT pwd FROM `loginschema`.credentials WHERE uname =" + '"' + uname + '"';
connection.query(DBuserCheck, function (err, data) {
if(err) {
res.send(err) : res.json({ USERdata: data })
console.log('user query data: dataSTR', +data + " ; req string " + req.body.post + "; uname string " + uname);
} else {
connection.query(DBpwdCheck, function (err, pdata) {
if (pdata == req.body.password) {
res.json({ Successfully_loggedin });
} else {
(err) ? res.send(err) : res.json({ pwdData_Wrong_Password: pdata });
console.log('enter valid credentials');
}
});
}
});
}
catch (ex) {
console.log('Message from Catch is ----->>>> ' + ex);
}
});
This solved my issue. I added if statement like this .
if(loginUserId ===stat.userId && singlePost.id){
return Object.assign({}, {
stat_id: stat.id,
is_status : true,
})
} else if(loginUserId ===userId){
return Object.assign({}, {
stat_id: stat.id,
is_status : true,
})
} else {
return Object.assign({}, {
stat_id: stat.id,
is_status : false,
})
}

nodejs events.js:141 unhandled error event ETIMEDOUT

I have a Heroku server with node.js and express that pings a website's API every second. This works fine for hours at a time, but every once in a while I'll get this error:
2018-01-10T02:19:28.579566+00:00 app[web.1]: events.js:141
2018-01-10T02:19:28.579578+00:00 app[web.1]: throw er; // Unhandled 'error' event
2018-01-10T02:19:28.579579+00:00 app[web.1]: ^
2018-01-10T02:19:28.579581+00:00 app[web.1]:
2018-01-10T02:19:28.579582+00:00 app[web.1]: Error: connect ETIMEDOUT 45.60.11.241:443
2018-01-10T02:19:28.579583+00:00 app[web.1]: at Object.exports._errnoException (util.js:907:11)
2018-01-10T02:19:28.579584+00:00 app[web.1]: at exports._exceptionWithHostPort (util.js:930:20)
2018-01-10T02:19:28.579585+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1078:14)
2018-01-10T02:19:28.684990+00:00 heroku[web.1]: Process exited with status 1
Sometimes the error is ETIMEDOUT but sometimes it's other things (can't remember right now).
Some other post I read made me think maybe this is a problem?
app.listen(app.get('port'), function() {
console.log("Node app is running at localhost:" + app.get('port'))
})
Or maybe it's the part inside the API call loop?
try
{
async.series([
function(callback) {
apiQuery( callback, method, params);
},
], function(error, results) {
console.log(results)
});
}
catch(e)
{
console.log("something went wrong!")
}
Not sure why the try catch isn't catching the error if it's this.
Maybe it's how I'm starting the loop?
runLoop()
//start looping the api data pulls
function runLoop() {
setInterval(apiLoop, 1000)
}
Would it be better to have apiLoop call itself in the callback function? Or would that create nested functions that keep using increasingly larger memory?
Here is the api call code:
function apiQuery( callback2, method, params )
{
if ( ! params ) params = [];
var host_name = 'www.host.com';
var url = '/Api/' + method;
if ( params ) url += "/" + params.join('/');
var options = {
host: host_name,
path: url,
};
callback = function(response) {
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
return callback2(null, str);
});
}
https.request(options, callback).end();
}
Maybe ETIMEOUT is caused by the website's server, anyway you can catch the error
const req = https.request(options, callback)
req.on('error', (e) => {
console.error(e);
});
req.end();

Node.js cluster error

Hello i am very new to node.js and javascript, i am trying to create a culster.js with the nodejs cluster module, at the end of my if statement i am calling server.js to start the app.
cluster.js
const cluster = require('cluster');
const cpuCount = require('os').cpus().length;
const startServer = require('./server');
if (cluster.isMaster) {
for (let i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
cluster.on('exit', () => {
cluster.fork();
});
} else {
return startServer;
}
server.js
const fs = require('fs');
const path = require('path');
const express = require('express');
const auth = require('http-auth');
const {
createBundleRenderer,
} = require('vue-server-renderer');
const bundle = fs.readFileSync('dist/server.js', 'utf-8');
const renderer = createBundleRenderer(bundle);
function parseIndexHtml() {
const [
entire,
htmlOpen,
htmlOpenTailAndHead,
headCloseAndBodyOpen,
bodyOpenTailAndContentBeforeApp,
contentAfterAppAndHtmlClose,
] = fs.readFileSync('index.html', 'utf8').match(/^([\s\S]+?<html)([\s\S]+?)(<\/head>[\s\S]*?<body)([\s\S]+?)<div id="?app"?><\/div>([\s\S]+)$/);
return {
entire,
htmlOpen,
htmlOpenTailAndHead,
headCloseAndBodyOpen,
bodyOpenTailAndContentBeforeApp,
contentAfterAppAndHtmlClose,
};
}
const indexHtml = parseIndexHtml();
const app = express();
const basicAuth = auth.basic({
realm: 'Jobportal',
}, (username, password, callback) => {
callback(username === 'x' && password === 'x');
});
app.get('/ping', (request, response) => {
response.status(200).end();
});
app.use(auth.connect(basicAuth));
// serve pure static assets
app.use('/public', express.static(path.resolve('./public')));
app.use('/dist', express.static(path.resolve('./dist')));
app.get('*', (request, response) => {
const context = {
url: request.url,
};
renderer.renderToString(context, (error, html) => {
if (error) {
if (error.code === '404') {
response.status(404).end(indexHtml.entire);
} else {
response.status(500).end(indexHtml.entire);
console.error(`Error during render: ${request.url}`); // eslint-disable-line
console.error(error); // eslint-disable-line
}
return;
}
const {
title,
htmlAttrs,
bodyAttrs,
link,
style,
script,
noscript,
meta,
} = context.meta.inject();
response.write(
`${indexHtml.htmlOpen} data-vue-meta-server-rendered ${htmlAttrs.text()} ${indexHtml.htmlOpenTailAndHead}
${meta.text()}
${title.text()}
${link.text()}
${style.text()}
${script.text()}
<script>
window.__INITIAL_STATE__ = ${JSON.stringify(context.initialState)}
</script>
${noscript.text()}
${indexHtml.headCloseAndBodyOpen} ${bodyAttrs.text()} ${indexHtml.bodyOpenTailAndContentBeforeApp}
${html}
<script src="/dist/client.js"></script>
${indexHtml.contentAfterAppAndHtmlClose}`
);
response.end();
});
});
const port = 8181;
// start server
app.listen(port, () => {
console.log(`server started at port ${port}`); // eslint-disable-line
});
I get an error
server started at port 8181
events.js:163
throw er; // Unhandled 'error' event
^
Error: bind EADDRINUSE null:8181
at Object.exports._errnoException (util.js:1050:11)
at exports._exceptionWithHostPort (util.js:1073:20)
at listenOnMasterHandle (net.js:1336:16)
at rr (internal/cluster/child.js:111:12)
at Worker.send (internal/cluster/child.js:78:7)
at process.onInternalMessage (internal/cluster/utils.js:42:8)
at emitTwo (events.js:111:20)
at process.emit (events.js:194:7)
at process.nextTick (internal/child_process.js:766:12)
at _combinedTickCallback (internal/process/next_tick.js:73:7)
events.js:163
throw er; // Unhandled 'error' event
^
Any ideas why ?
EADDRINUSE means that the port number which listen() tries to bind the server to is already in use.
You need to verify if the port is already taken on your system. To do that:
On linux: sudo netstat -nltp | grep (port) in your case is port 8181.
On OSX: sudo lsof -i -P | grep (port)
If you have a result, you need to kill the process (kill <pid>).
You should check if pm2 list returns 0 process. In addition, when you do a pm2 stopAll, the socket is not released. Don't forget to do a pm2 kill to be sure the daemon is killed.
$ pm2 kill
Daemon killed
Verifying for Windows:
C:\> netstat -a -b
a Displays all connections and listening ports.
b Displays the executable involved in creating each connection or listening port. In some cases well-known executables host multiple independent components, and in these cases the sequence of components involved in creating the connection or listening port is displayed. In this case the executable name is in [] at the bottom, on top is the component it called, and so forth until TCP/IP was reached. Note that this option can be time-consuming and will fail unless you have sufficient permissions.
n Displays addresses and port numbers in numerical form.
o Displays the owning process ID associated with each connection.
EXAMPLES to kill in windows command line:
If you know the name of a process to kill, for example notepad.exe, use the following command from a command prompt to end it:
taskkill /IM notepad.exe
To kill a single instance of a process, specify its process id (PID). For example, if the desired process has a PID of 827, use the following command to kill it:
taskkill /PID 827

Categories

Resources