Catbook Workshop: Intro To Nodejs: Monde Duinkharjav
Catbook Workshop: Intro To Nodejs: Monde Duinkharjav
Intro to NodeJS
Monde Duinkharjav
What is NodeJS?
NodeJS is...
A Javascript RUNTIME ENGINE
NOT a framework
In javascript, we have...
Node Package Manager (NPM)
Initializing a Javascript Project using NPM
$ npm init
$ nodejs app.js
Hello World
Let’s create a web server using the http package
$ npm install http --save
Why? You don’t want to push thousands of lines of code from libraries into your
repository. Instead, you make git ignore the node_modules directory.
When you later clone your project code, you always run
$ npm install
$ nodejs app.js
If you’re lost:
Go to go.6148.io/workshop2 and clone
git checkout step1
function(req, res) {}
1. req stands for request, res stands for response
2. We’re passing in a function to these methods:
Whenever a request is made, this function is invoked.
The argument res is mutated by this function.
3. This type of function pops up a LOT in web server request handling
Let’s test our web server out
Open your web browser: http://localhost:3000
Okay, we’re done! We have a web app. Thank you for coming. You’re done…
Let’s test our web server out
Real applications are many orders of magnitude more complex so the code base
can get out of hand really quickly.
Introducing: Express
Exercise: Create a new endpoint called ‘/u/profile’ that sends the message ‘My
Profile’.
If you’re behind, you can run to hop in right before you need to do this exercise
‘/aaronsipsersucks’ or
‘/dannysrealnameisdanielwinstontang’
Okay moving on… more Housekeeping
What happens if we try to reach the endpoint
‘/aaronsipsersucks’ or
‘/dannysrealnameisdanielwinstontang’
404!!!
Okay moving on… more Housekeeping
We should also take care of 500
Internal Errors.
module.exports = …;