File sharing using socket.io and nodejs - node.js

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

Related

Nodejs crashing silently when trying to aceess file system with "fs"

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.

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

How to use the pg node package in angular

Situation
Hi, I'm quite new to Angular, I've been doing some projects following tutorials, which then lead me to try to start my own project to practice my Postgres and newly acquired Angular "skills".
I am trying to do a webapp that connects to a postgres DB using the node pg module.
(I know sequelize is a thing and it seems to work better than pg but AFAIK sequelize doesn't let you run pure postgres commands through it) Please correct me if I am wrong about this
The problem
This is where I get stuck, I am trying to follow the instructions from the docs but it doesn't seem to work correctly.
I have tried:
const { Client } = require('pg');
import { Client } from 'pg';
Also tried importing it in the .angular-cli.json in the scripts array
All of these fail with errors similar to this
ERROR in ./node_modules/pg/lib/connection-parameters.js Module not found: Error: Can't resolve 'dns' in '[...]\node_modules\pg\lib'
ERROR in ./node_modules/pg/lib/native/client.js Module not found: Error: Can't resolve 'pg-native' in '[...]\node_modules\pg\lib\native'
But nothing seems to work properly. Am I doing this completely wrong?
Also, pretty dumb question. I believe angular does everything on the client side, this is a HUGE security risk for DB access in prod. If that is true, is there a way to write server-side .ts services? or are services server-side?
You could write your serverside code in node using compiled ts, but probably not with angular.

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

Calling server-side code from client on Derby.js

I'm new to using Derby.js, and have scaffolded out a project using the yeoman generator-derby package. I thought everything was going fine, but I can't figure out what I'm doing wrong here.
A breakdown:
I have an 'app/dbWp.js' controller that exports several functions, and requires the modules 'mysql', 'async', and 'needle'
In my app/index.js, I import this file and use it like so:
app.proto.submitWp = function() {
dbWp.createUser(this.model);
};
I call this function from the view/index.jade like so:
button.btn.btn-primary(type="button", on-click="submitWp()")
In the browser, I get numerous console.error message complaining about the 'fs' module not being defined. After much googling, I discover that it's due to Browserify ignoring the 'fs' module, which subsequently causes problems with modules 'mysql' and 'needle'. But that implies this code is being executed in the browser?
So my question is: why is this trying to call the function on the client side? Obviously if it executes on the server side, as I thought it was going to, there wouldn't be a problem requiring these modules.
How can I execute this function on the server? Had this working fine with express + socket.io before, but wanted to change frameworks and give Derby.js a shot.
I'm clearly misunderstanding something about how Derby.js is supposed to work; any help would be appreciated. Thanks!
I know this is like 4 months later, but being new to DerbyJs too, I thought I could try and help.
I personally with standard html code have the equivalent working.
<button on-click="editContact(#contact.id)">Edit Contact</button>
This indeed runs code on the server. Can you try writing your code in standard HTML, or perhaps better yet, see if you can do a console.log on the server method to see if it even is getting there?
Perhaps the best would be to call an empty function on the server with a console log and check both the browser console and the server console.

Resources