The process of Nodejs running a js file - javascript

I am new to Nodejs world.I have some questions.Why the node service do not need to start while the apache service need to start when i use the command node xxx.js it can give me the result.How can the node do this?What are the steps of this process.

node is a program that contains the Javascript interpreter. To run a script file, you run node and pass it the script you want to run on command line. It initializes itself, then loads and runs the desired script.
node is more generic than Apache. It is not, by itself, a server of any kind. If you want a node.js app to be a server, you have to start a server yourself in your Javascript code.
Apache is a web server. When you start it, it starts a web server and then you can run things in the context of that web server. Apache does not contain it's own language interpreter like node does. It can run other types of code like PHP, but you have to supply it with a PHP interpreter in order for it to do that. node, on the other hand, has a Javascript interpreter built in.
While node can be used to create a web server by writing a Javascript script that creates and starts a web server, node is more generic than Apache. node can be used for all sorts of non-web server types of things. For example, I have a nodejs script on my computer that runs automatically each night that cleans up a bunch of automatic backup files on my disk by aging them (files older than a week are deleted). That isn't something you would do with Apache.
So, while there's some functionality overlap only because you can create a web server with node.js, node and Apache are fundamentally different types of tools.

Related

How can I run shell scripts on a remote linux server using an Angular(8) UI?

So, I have been doing MEAN stack web development for some time now. I am normally used to Angular UI development and executing CRUD operations using node servers.
Now, I have a new requirement - Create a UI for running some shell scripts on a remote Linux server.
For example:-
files are located in a path such as files/bin/example.sh in the remote server.
I have to be able to perform operations via my UI such as:-
example.sh status device name
example.sh start device name
How can I do this? Can anyone give me some directions?
Thanks.
You can use this shelljs npm package to do that. you will have to write a REST API that will execute your shell script from the server-side (Using node js).
ex
const shell = require('shelljs')
shell.exec('./path_to_your_file')
And then you can call that REST endpoint from your angular application.

Are all node.js modules server-side?

Is there front-end node modules or all are server-side unless packaged using browserify and similar tools?
Node.js modules are designed to be used with Node.js.
You might run Node.js as a server side environment. You might run it as a command line program. You might run it as an HTTP client in its own right.
Node.js does not run inside a browser. Node.js modules are not generally designed for use embedded in a webpage using <script> elements.
It is possible to write a hybrid JavaScript file that can function both as a Node.js module and as a script in a webpage … but there isn't often a good reason to do so. (I've done it once: To write a client for a particular webservice that I wanted to use in a browser and in a Node.js program).
Browserify can convert some Node.js modules so they can run in a browser.

How do I deploy Node.js applications as a single executable file using systemd?

I want to deploy my node application as single executable file, is it possible by using systemd, containers. I dont have enough knowledge on systemd and containers. Please help me if anybody knows about it.
Where i work we use pm2 to run NodeJS applications.
It allows you to run multiple instances on one server, monitors them and restarts if needed (on failure or you can provide a memory limit).
If you insist going with systemd, you will have to create a unit - a file that describes the execution path of you application for systemd.
It would usually be in /usr/lib/systemd/system
You would have to create a file ending with ".service"
[Unit]
Description=My NodeJS App
[Service]
ExecStart=/usr/bin/node /path/to/my/app/index.js

How to make an executable(.exe) that packages a nodejs server? (not UI-centric)

I have implemented a nodejs server that serves incoming requests to use the bluetooth services of the local computer. I want the nodejs server to be packed as an windows executable file so that I can distribute it. People should be able to just install/run that .exe which will install any packages required (if any) and run the server. How to do this?. I saw and tried node-webkit etc., but they are UI-centric, that is it can pack a nodejs application that opens a html page. But I want my server javascript file to be executed, like the way it is done in command prompt : node file-name.js. How to do this?
I've a server running with nodejs and for execute this I use a .bat file.
Create a .bat file
Inside of the file put:
cd path/to/server/
node índex.js
I used JXCore for this task in the past. It basically creates one executable that includes everything.
Unfortunately active development of is halted.
Solution:
You can use nexe for that.
Create a single executable out of your node.js app
Motivation
Ability to run multiple applications with different node.js runtimes.
Distributable binaries without needing node / npm.
Starts faster.
Lockdown specific application versions, and easily rollback.
Faster deployments.

Running a local server with javascript

I want to create a simple Javascript program with a HTML interface. The program will run in Chrome. I will also use node-serialport and Node.js to comunicate with an Arduino. I have a HTML and JavaScript file done, but I have no clue how to run it, or how to implement Node.js or node-serialport, nor how to "start" the sever. Initially it will only be running locally, but eventually it may become a real sever. For now, how do I run all that locally?
EDIT: I'm using the sample code from http://brandontilley.com/2012/03/02/controlling-an-arduino-from-nodejs.html, with the CoffeeScript converted into JavaScript.
Lucas, glad you found the blog post useful; perhaps I should add this information to it.
Getting the sketch into your Arduino
Just fire up the Arduino application, paste in the sketch code, and hit "Upload." Should be all you need to do here.
Starting the Node.js Server
What operating system are you using this on? Finding out how to access your Arduino microcontroller via node-serialport will differ based on your OS.
In the source code, change the string value of port to be your Arduino's device (once you know it). Also, the script depends on Express and (of course) node-serialport from NPM, so run npm install express serialport in the directory where your JavaScript file is saved. Finally, run the file with node server.js (assuming server.js is the name of your file). Then you can access the server at http://localhost:8080.
You can use node.js to serve up HTML with Express. If your main Javascript file is called server.js, then run it by typing:
node server.js
at the command line.

Categories

Resources