Can we get running js script details in nodejs - node.js

This query is more related to nodejs(compared to particular os) thats why posting it here.
I have searched for similar problem, but I did not get any satisfactory results.
I want to know that Is there any way that we can figure out the JS Script(name, location) node is running currently?
e.g.
If I am running a nodejs webserver on port 3000, and if I forget the original script location, or somebody else start using my computer.
So, is it possible using nodejs to get the location of this script.

Try the global variable __filename, as detailed here.

Related

Update a web page using socket.io when database is updated by another page

I'm starting playing with NodeJS and Socket.IO and I've got a question regarding communication between pages of my application.
I've got a Node JS script (get_new_data) which is periodicaly executed using a cron task. This script will get information through Internet, parse them and write them in a PG Database. This is ok.
I've got a secondary script (show_last_data) using Node/Express/Socket.io that displays the last information of the DB when it is executed. This is also ok, right now.
What I would like now, is that the script show_last_data would be notified when new data are inserted in the database and the corresponding display divs to be updated.
Can get_new_data connect to show_last_data even if it is not the server that produces the page ? Or should I manage this in another way ?
Thanks for help
I've finally found a way to manage this using socket.io-client.
This page was particulary helpful to solve my question : socket.io as a client

fs.readFileSync is not a function Meteor, React

I'm getting a 'fs.readFileSync is not a function' in Chrome debugger after trying to call readFileSync();
I call it...
const fs = require('fs');
call the function...
let content = fs.readFileSync('/path/to/my/file.stuff');
And attempt to display content..
console.log(content);
I get nothing. When I do...
console.log(fs);
I appear to get a generic javascript object...
I'm completely stuck.
Meteor version: 1.5.1
npm version: 3.10.10
node version: v6.10.1
Thanks for all the answers!
I have confirmed that you cannot use fs on the client side.
Instead, I made another local simple express node api and the react web app just makes a request back to the node api to get that data.
Also, you have to do this...
https://enable-cors.org/server_expressjs.html
EDIT:
Wrote this a long time ago. 3 years back when I was just starting my web development learning. Just want to update and say that there is a serious fundamental difference between what the user sees and what the server sees. Allowing the front-end (Meteor, React, Angular, etc.) to read files would be a super serious security issue. Anyone could make a website that when a user goes to it, it would just read your local computers files. Not good...
While this is super obvious to me now, it wasn't obvious 3 years ago. So for all you newbies out there, it's okay :) No question is a dumb question.
I'm getting a 'fs.readFileSync is not a function' in Chrome debugger after trying to call readFileSync();
fs will not work in the browser. This is by design as to protect your filesystem from potential security threats.
Using low level Node packages in a browser environment
If you need access to this in a browser environment, consider making use of Electron which allows you to make use of OS level NodeJS packages in a running instance of Chromium.
fs cannot be used on the client, due to browsers restricting some javascript code.
If your code is being run on both the server and client, you can use:
if (Meteor.isClient) return;
to avoid the error. Otherwise, there should be another way to do what you're trying to accomplish, such as importing required JSON.

Can't require('fs') in ember application

I'm having trouble adding the line require('fs') anywhere in ambari-web. I've tried adding it in ambari-web/app/app.js, ambari-web/app/controllers/wizard/step3_controller.js, and other places. Every time, I end up with
Uncaught Error: Cannot find module "fs" from "app"
or something similar in Chrome's console log. What is going wrong, what misunderstandings do I have, and how can I add the fs module to this ember application so that I can use it in a controller? I've tried running following all of the build instructions again after adding the module too -- without any luck.
Edit: is this a bad question? I'm getting downvoted, so let me know how to improve it.
fs is a Node.js module that is not available in browsers. There are various ways to emulate it in browsers, depending on what you're trying to do, but most likely to accomplish what you're attempting you'd need a separate Node.js program running that the web app makes requests to in order to trigger the file system operations you want to do.

How to access NodeJS data through cakePHP?

Dummy question here but I haven't found any answer on the web for now.
I'm working on a cakephp website, installed on a Raspberry Pi, which is supposed to be able to connect wireless-ly through a local network to an Arduino YUN, get its components list.
For example :
"A LED is connected on ports :
Input : 2,
Output : 6;
and is currently on"
And change the input value :
"turn the LED off"
I'm not supposed to reload the webpage to see the change occurring.
So I figured out I needed NodeJS to send/receive the informations with websockets but I don't know how to connect NodeJS -running on its own webserver- to cakephp.
I'm a complete rookie with NodeJS, just did a few tutorials earlier, so I'm stuck here.
Does anyone know how to deal with this ?
Thanks in advance,
There are a few different libraries you can use to connect to the node server.
http://elephant.io/
https://github.com/bergie/dnode-php
You can, of course, just fall back to sending http requests (curl) from your cakephp app to the node server.
I have a few discoveries to add that may help some people.
While searching more deeply on DNode and elephant.io I found a cakePHP plugin called cake_websocket which make use of socket.io.
Seems to me that it is an equivalent to elephant.io specialized in cakePHP (while elephant.io is 'just' PHP).
I hope it can help someone !
Instead of using any library.
You can try iframe tag with src to your 'node.js' view.html.
So, listen for events on view.html which are triggered from your 'cakephp' view.html
So, you will have cakephp->view.html talking to node.js->view.html which is connected to node.js->index.js(server)

backboneJS application runs fine in NodeJs but not in any other server?

I was trying to load a json file in Collection of BackboneJS. While trying to run this through nodeJS server, it works fine. But once put in a server environment like XAMPP or even in a remote server it does not work? Any link would be a nice help. ( For reference my code is at github : https://github.com/saumya/backboneJS-basics )
thanks
Hey thanks for the helping hand.
Probably I did not understand the situation, so the question is not correct as I see now.
The modified question is:
I have a frontend Application running on NodeJS server at some port.
I am trying to load JSON from another server running on another server.
The answer of #Floby is the way I solved it. Its the CORS thing on the server which provides the JSON. It took me sometime to understand what is happening.

Resources