February 21, 2020
Javascript - node.js
In a nutshell, node.js is a way to run javascript outside of a browser.
This is useful for two things.
One is that you can write javascript programs much like you might write
perl or python programs that have nothing at all to do with the web
or browsers.
Second is that you can write "server side" javascript that acts like a
web server, usually on some separate port.
Installing node
This varies according to whether you are running mac or linux or some other
operating system.
In my case I am running linux Fedora 30 and I can install node and npm via:
su
dnf install node npm npx
Note that "npx" can also be installed, which may be useful.
The "npm" program is the "node package manager" and is used to install other
node packages. There are lots of them! You may have the choice (as I do)
of whether to use DNF or NPM to install these packages. Suit yourself.
I don't have solid recommendations on this issue.
The pros and cons arise when you want to stay up to date with new versions.
Where to go from here
A very simple node program
Remember that node is just javascript standing on its own two legs without
having a browser wrapped around it. Let's begin with a one line program
that prints "hello world". Put the following into a file hello.js
console.log ( "hey" );
Now type (at the command line) "node hello.js" and you should get:
hello world
It doesn't get much easier than that.
Using node as a web server
This is much simpler that it sounds.
Put the following into a file "hello_server.js"
Then run it as "node hello_server.js".
This will start the program running as a server on port 8888.
You can use either of the following to point your browser at this
server:
Either works because the machine "localhost" is almost always configured to
be 127.0.0.1
There is a lot going on in this short file, but we are happy at this point to
just ignore the details and be happy to be able to start and test a simple server.
Have any comments? Questions?
Drop me a line!
Adventures in Computing / tom@mmto.org