Crash Course in NodeJS
(This is a very thrown together, last minute talk. Please forgive everything about it.)
This is line 4. (Might be relevant later)
What is NodeJS?
What is NPM?
If you've done back-end things in other languages, you may be familiar with using libraries or dependencies. Basically this is code someone else has already written, to do common things that make your life easier.
Some basic commands to know
- npm init
- npm install --save
A node project will have a 'package.json' file which holds dependencies and some other information.
We're going to be using Cloud9, a cloud based IDE, to run these demonstrations. If you'd like to code along with me, please go ahead and create a free account at c9.io
Later if we have time, we can talk about downloading node and running it locally on your machine.
Start a Cloud9 workspace, and git clone this repo.
*** Finish instructions ***
I want to show you something cool about the commands we're typing in. We actually can access the commands from inside the file. When I console log 'process.argv' we get back an array. One is the file path to node, which is the process we're running. The second is the path to the file node is executing. The first and second commands are available here as arrays.
Just for fun, let's add a third command, any word. We can see that get console logged too, in the second index of this array. Because that's accessible from inside the file, let's see what we can do. Let's say hello to a name that we provide.
Cool, so we were able to access the terminal commands inside our file and use them as input.
Now we're going to use our first node library. This comes baked into Node already, and you can review the documentation at https://nodejs.org/dist/latest-v6.x/docs/api/. We're going to pull in the 'fs' library to be able to read a file.
We start by declaring a variable and setting it to 'require' the 'fs' library. I'm basically pulling in an Object that has a method I want to use, which reads a file. This method returns a buffer object, which we're going to convert to a string and then split into an array separating each line.
Now we're going to make a simple web server. For that we're going to bring in 'http', a baked in node library for handling web requests. The require method saves our library into a variable we can use.