call to functiolity just one time in node js - javascript

There is method which I need to run just once when the node application is started ,I think about to put it in the server.js which is my executable file to the application but Im not sure that this is the right way to go,which other option I can use in this case?

Depending on when specifically it's required to be run, it may be suitable to put the function into a separate javascript file, and execute it as part of your npm start script within your package.json?
Based on your comment below stating it is a child process. It would be appropriate to execute it from the server file, but best practice would be to abstract the function out into a separate file, require it into your server file and then call it.

Related

Not able to fork external JavaScript server file in Electron

so I am making an application that requires a backend API, and it uses certain node_modules which don't work when compiling with Electron. To fix this, I put the API code into a separate JavaScript file, which I am attempting to fork using child_process.
I have gotten this to work when compiling, but it immediately stops working after I move the "win-unpacked" folder or try to install the app using the compiled installer.
I have checked, and it is not the path that is wrong, it is correctly pointing to the file. From testing, it appears that the file actually does get forked, but immediately exits with the status code 1.
I can't use require(./filepath.js) because that will just include the code in the compiler, which doesn't work with the modules I am using.
I am hoping someone knows what is wrong and what I should do to fix it, or have any ideas for other ways to run the server code without including it in the compiler.
I am using Vue.js 3 and vue-cli-electron-builder version 2.1.1
The server I am attempting to run is a express server.

Run function in running process on node js using command line

I would like to know if it's possible to run a specific function on a running process on Node JS. And how to do this
actually im using
npx run-func server.js functionName
but it completely restarts the file.
For information, I can't split the file in two since I use a database that can only be read by one process. So... If you have any advice please

How to see Node method path/file

I see several node module js files scattered through node_modules directories in my computer.
How can I see the path taken to execute http.request (or any other method).
I want to see the code for the methods I am calling (where it lives on my computer).
Also wondering if the code executed when I call a method in node is JS, binary, or both.

How to read (text) files using NodeJS

I am working on a project using AngularJS and NodeJS. I am new to both frameworks and need some help for reading in (text) files.
I am trying to read files using npm's read-file (https://www.npmjs.com/package/read-file).
I put var read = require('read-file') into one of my controllers Javascript files, but I keep getting an error message Cannot find module 'read-file'
Below is the current project structure:
my_project
|--app
|--bower_components
|--components
|--scripts
|--controllers
|--my_controller.js
|--styles
|--views
|--my_page.html
|--app.js
|--index.html
|--node_modules
|--read-file
|--index.js
|--LICENSE
|--package.json
|--README.md
Clearly I installed the read-file module, but I think there is an issue locating it. What should I do to make sure that require('read-file') correctly locates the read-file module?
Any help will be appreciated. Thanks!
It seems that you are trying to read the read-file module within your Angular.js application controller. The controllers within Angular.js are front end files and are run from within the Browser environment and not Node.js. Therefore, you cannot use utilities such as require within the browser.
I think you may be able to achieve what you are trying to accomplish by running your front end application through a tool like Browserify. This will allow you to use Node.js to do stuff like require which will load the file during compilation time and allow you to require files within the browser. You can also use Node.js tools such as fs to load the contents of files and include them within your front end application. I think this should get you pretty close.
I think you are trying to load the read-file module in the client js part, where is not defined at all.
You should create your node server here, and create an api you should call from your front-end controller that load get the file.
From the node api handler you read the file and send back to the client when requested.

Running a Node.JS script from any directory?

How can I run Node.js like node? at the moment I have to Cd into the directory of my js script, is there a way to run it from any where?
Just run the command and include the full file path:
# alex # alex in ~ [16:53:43] tty:s002 L:1 N:14 C:0
$ node /full/path/to/my/application/file/index.js
There can be possible answer like the one suggest by #ajtrichards, its one way to achieve what you like.
In other ways you can follow full path approach and create a .bat file, one time effort, after that you need only to click that file to run your node app.
Other way, If supposed in your app structure your server folder is of different shape i.e. main file is located at inner leve (its possible in some cases), you can configure package.json to handle your server execution with npm start.
Above are just possible suggestion.

Categories