Nodejs crashing silently when trying to aceess file system with "fs" - node.js

I'm currently working on an Nodejs API server with Express. and on a specific path i had this,
const fs = require("fs/promises");
fs.mkdir(folderPath, {recursive: true})).then(console.log).catch(consolle.log);
"folderPath" is the full path of the folder that i want to create. now when the execution reaches this line the NodeJs server crashes silently without any trace, nothing in the log files, nothing in the try/catch block for the code, nothing in the Express error handler. The request to that specific route will get a 503 error, which is not from express, and i think this error originates from the server it'self after NodeJS crashes.
To make sure that the error is not caused by the parameters passed to fs.mkdir i specifically put the above lines of code into the entry file (index.js) where express app is started, and replaced "folderPath" with an actual path that i'm sure exists.
const fs = require("fs/promises");
fs.mkdir("/home/abcd/efg", {recursive: true})).then(console.log).catch(console.log);
And the NodeJS server crashes as soon as i start it, with out any trace just like i mentioned above.
The next thing i tried is running a "test.js" file with the above lines of code from the terminal as, test.js only contains those two lines of code.
node test.js
test.js is located within the same folder as index.js, and surprisingly it works. the folder get's created without no error, which leaves me confused. Why would it fail when running from index.js, but works fine when running from test.js.
what am i doing wrong here that's causing the crashes, is there a permission thing that i'm not aware of, or is it something else.
here is some detail of the server:
I'm using Node 14.7.0. One other thing is that when the sever crash occurs large size files named "core####" (eg core3424) are created in the api root folder, where index.js is. and also Number of processes and Memory usage Max out.

Related

Make a logger for Node Js

I have a project in Node Js, which executes the project on port 3000 and I access from ngrok with my browser to said localhost port, and it executes a server on port 3001 to make requests to a Maria database db. The project is done in react and the server with express.
I want to save the application logs (errors, warnings, etc.) in a log file so that I can see them whenever I want.
My intention was to use winston, and while I have no problem on the server side (3001), when I try to adapt it to the main project, I get an error that it cannot save files (the reason that appears is that it runs from the browser, and you can't create such a file because you don't have access to the project folders)
Can anyone give me some advice? Am I wrong to use winston, and should I use another?
Greetings and thanks
I've never used winston before and I couldn't find anything online about your error. In the past I've always just used node's fs module to create a log of errors and restarts.
const fs = require('fs')
Node's File System Documentation: https://nodejs.dev/learn/the-nodejs-fs-module
Short YouTube Tutorial: https://www.youtube.com/watch?v=U57kU311-nE

NodeJS fetch returns ECONNREFUSED error when executed in fs.writeFile callback

I have some code where I have generated some json data, then I would like to
write it to a file, then
hit an api endpoint (a get request)
The issue I am running into is:
when I execute fetch after using fs.writeFile I get an ECONNREFUSED error. If I do not write the file, my get request to the endpoint is successful.
I am putting my fetch in the callback to the writeFile function - I have also tried fs.writeFileSync(url) which gives me the same results. My full code requires writeFile to come first.
I noticed if I wrap fetch in a setTimeout with 10000ms, then fetch will work. It seems as if writeFile isn't waiting long enough to execute the callback function.
Am I doing something incorrectly? or How do I correctly write a file and then subsequently fetch API data?
I boiled down my code to the most minimal example to reproduce this behavior - as well as allowing node to correctly return the error messages. (using a fake URL for this example as the real url isn't publicly accessible)
const fetch = require('node-fetch');
const fs = require('fs');
try {
fs.writeFile('./example.json', JSON.stringify(['test', 'one', 'two']), () => {
fetch('http://www.example.com/api_endpoint?q=test')
.then(console.info)
.catch(console.error);
});
} catch (e) {
console.info(e);
}
I'm running this in nodejs v10.15.1 on Linux Debian 8.11 (jessie)
I found out the problem, it's real silly...
My script is in the same repo as my API server. When running the server in dev mode (adonis serve --dev), fs.writeFile triggers the file watcher to reload (of course), which temporarily disconnects the server. It is very obvious now why it's not working.
The solution was to have the file watcher ignore the folder I am writing the json file to.
In my case (working with adonisjs) that is adonis server --dev -i scripts
Oddly enough, this is a project that worked a month ago and I didn't have this issue then. I guess something changed in how I'm running it between then and now.

File sharing using socket.io and nodejs

When I wanted to do file sharing I'm stuck up with this error
The error is fs.creatreadstream() is not a function ? Am i missing out anything even after performing browserify for the client side and using require('fs')
I'm following the below documentation!!!
https://github.com/nkzawa/socket.io-stream

node karma fs object empty

I'm trying to run some tests on node using karma. I'm running using both phantom and real browsers.
Whichever way I run I get an error on fs read file functions.
'undefined' is not an object (evaluating 'fs.existsSync')
This is even if I have a very simple file like:
var fs = require('fs');
console.error(fs);
var text = fs.readFile('data.txt', 'utf8');
The first console writes out Object {}. The second one gives me the above error.
I'm assuming that the object is empty.
I'm using the latest version of karma and dependencies.
Can anybody point me in the right direction as to why the fs object is empty/not working.
Karma is the client side js test runner you cannot use node file system on it. To test server side js I suggest to use a different test runner like Mocha. If you use Mocha for the server js namely nodejs you will be able to use node filesystem.
There is another way to read file from the client is using XMLHttpRequest(). Refer you to this question
Javascript - read local text file

No entries in log file when using logging library

I do not know exactly if this is a issue resolving around forever or the libraries I tried but maybe someone can help here.
I was using console.log() through my application (express) to write logs. Now I've switched to a more feature packed logging library, namely Winston.js. Configured winston like this:
var winston = require('winston');
winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, {timestamp: true});
and replaced my console.log() entries with winston.info/winston.error, you name it. After I launched my app with node app.js everything went as expected. So I tried to run it with forever, since in production I run it that way.
Now I encountered the problem that the whole log file from forever, using just forever start app.js, was empty. Not a single log entry was there.
I replaced winston with log4js, but the log file remained empty when launching it with forever.
Is there anything I am missing right now and if so where is the problem?
Sincerly,
cschaeffler
It looks like you simply kept the Console transport without adding a transport for a file, e.g.,
winston.add(winston.transports.File, { filename: 'somefile.log' });

Resources