Skip to content
No description, website, or topics provided.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
html-server
make-your-own-modules
simple-server
README.md
hello.js

README.md

Node.js for Beginners

Adapted from a Develop Denver Talk by Ryan Ciecalone: August 4, 2016

Objectives

By the end of my 'Active Recall' session, I will be able to...

  1. Identify and describe the benefits of Node.js
  2. Write a node program that logs 'Hello World!' to the console
  3. Create and access node modules using require and module.exports
  4. Create a simple server using Node.js
  5. Serve an HTML file to the browser using Node
Objective 1: Identify and describe the benefits of using Node.js
  • You can write server-side code in JavaScript! No need to learn a new language.
  • Node works well for real time apps that need a persistent connection between the client and server
  • Node works with multiple platforms: ios, Linux, and Windows
  • It is free and open-source:
    • There are lots of online tutorials
    • Excellent documentation
    • You can access an online community that will answer your questions
  • Node.js is fast, scalable, and asynchronous
    • Node is asynchronous meaning that multiple functions can work at one time.
      • When code is blocking, it means that a function must run to completion before another one can be invoked
      • When code is non-blocking, it (sort of) means that multiple functions can run simultaneously
      • A good way of explaining async/non-blocking code is thinking about (this)
Objective 2: Write a node program that will print 'Hello World'
  1. Download Node from nodejs.org, or install using Brew with these instructions
  2. Create a file called 'hello.js'
  3. Write the 'Hello World!' code. For extra fun, use a setTimeout, and have 'World!' appear a few seconds after 'Hello.'
  4. Run the hello.js file in your command line: node hello.js
Objective 3: Create and access node modules using module.exports and require
  1. Make a new directory called 'make-your-own-modules'
  2. In the directory, create two different files: loud-greeting.js, and soft-greeting.js
  3. In loud-greeting.js, create an object named loud, and add a key called sayHi and a value of 'Hello There!'var loud = { sayHi: 'HELLO THERE!'}
  4. Create a soft greeting that returns a lowercase 'hello there' in the soft-greeting.js file
  5. Add your greetings to the module by adding module.exports = loud and module.exports = soft at the bottom of your loud-greeting.js and soft-greeting.js (if you want to examine the module, console.log it and run the loud-greeting.js file)
  6. Create an app.js file
  7. In the file, require my the soft-greeting and loud-greeting modules by declaring a variable, and assigning it the value the require function. Pass in the path the module as an argument. The argument is a string, so use quotation marks. (I made these modules, so the argument is the the entire path. If it was a node module, just the name is required)
  8. Access the loud and soft greeting objects: In the app.js file, grab the values of each by using dot notation soft.sayHi and loud.sayHi
  9. Use node to run app.js

Objective 4: Create a simple server using Node.js

  1. Make a new directory called 'simple-server'
  2. In the directory, create a new file called 'server.js'
  3. In the server.js file, use require to access node's built-in http module var http = require('http')
  4. On the http object, invoke the .createServer() method, and pass in a callback function with 2 arguments: request and response
  5. On the response, invoke the .writeHead() method and pass in a status code, for now 200
  6. On another line, on the response, invoke the .end() method on the response, and pass in a message in the form of a string
  7. On the end of the create server function, invoke the .listen() method, and pass in one argument, the port number
  8. Run your server.js file with node, and then check on localhost:port-number-you-specified-in.listen(). You should see the string you passed in in the .end() method

Objective 5: Serve an HTML file to the browser using Node

  1. Make a new directory called 'html-server'
  2. In the directory, create two new files: 'app.js' and 'index.html.'
  • Note: I was tempted to name the file 'server.js,' but we are building a mini-application: if you add a button or some other way of interacting with the page, you have both client and server side code. Woo! A full-stack app!
  1. In the index.html file, create a page you'd like to see in the browser. For this exercise, keep it simple.
  2. In the app.js file, use require to access node's built-in http module var http = require('http'), and file server module var fs = require('fs')
  3. on the fs object, invoke the .readFile() method and pass in two arguments: the path to the html file you want to serve, and a callback function that accepts the arguments of an error, and the file type, html. In the code block, write a conditional statement that if an error occurs, throw the error.
  4. On the http object, invoke the .createServer() method, and pass in a callback function with 2 arguments: request and response
  5. In the code block, invoke the .writeHead() method on the response. Pass in a status code, and an object that has a key of 'Content-Type' and a value of 'text/html'
  6. On the response, invoke the .write() and pass in html (the argument from .readFile())
  7. On another line, on the response, invoke the .end() method on the response.
  8. On the end of the create server function, invoke the .listen() method, and pass in one argument, the port number
  9. Run your app.js file with node, and then check on localhost:port-number-you-specified-in.listen(). You should see a rendering of your index.html!
You can’t perform that action at this time.