Recitation 2: Node.js
Overview
This recitation aims to highlight the basics of Node.js by walking students through the implementation of a basic To-Do app.
Staff Overview
- Nicholas Wernink & Alex Barrios
- Emails: ngw@andrew.cmu.edu & ambarrio@andrew.cmu.edu
- Office Hours: Wed 2:30 - 4pm (Nicholas), Thu 12pm - 1pm (Alex)
Full-stack Development
Different levels of the stack: - Backend - Frontend - Database - Deployment - Testing and more
BACKEND DEVELOPMENT USING NODE.JS
What is Node?
- Back-end JavaScript runtime environment, runs on the V8 JavaScript Engine
- Used for server-side scripting
- Has an event-driven architecture capable of asynchronous(non-blocking) I/O.
- Used to build fast, powerful and scalable web applications
Asynchronous or Non-blocking I/O
Every request consists of:
- Non-blocking I/O allows a thread to suspend a request while it’s performing I/O to go work on a different request
- Runs on a single-thread event loop, using non-blocking I/O calls, allowing it to support tens of thousands of concurrent connections without incurring the cost of thread context switching
- Concurrency (in Node) refers to the Event Loop’s capacity to execute Javascript “callback” functions after completing other work
Node js async options
- Callbacks: Functions passed to another functions
- Promises (and promise chains): Structured callbacks
- Generators: Functions which can be exited/paused and later re-entered
- Async/Await: Combining generators and promises
Node Package Manager (npm)
Consists of: - npm client - command line tool - npm registry - an online database of public and paid-for private packages
The largest ecosystem of open-source libraries in the world
Model View Controller (MVC)
Model View Viewmodel (MVVM)
A SIMPLE TO-DO APP USING NODE.JS
mkdir
into your project folder (todo-app) andcd
into itnpm init
npm i --save-dev typescript @types/node @types/express npm i cors dotenv mongoose
- Create tsconfig.json file
- Write code, as seen in either the MongoDB or Postgres reference solution
npm start
(to start the server)- (optional) Play with the server, send it post requests
Steps for building off of references
- Clone either the MongoDB or Postgres reference solution
- cd into server directory with
cd server
- Run
npm install
in command line - Run
npm start
in command line - (optional) Play with the server, send it post requests
Upcoming
- Download Postman to test your server/API
- Homework 2 due Thursday
- Meet with your group soon if you haven’t already!