Node.js. Node.js Overview. CS144: Web Applications

Similar documents
COMP 2406: Fundamentals of Web Applications. Fall 2013 Mid-Term Exam Solutions

Catbook Workshop: Intro to NodeJS. Monde Duinkharjav

NODE.JS MOCK TEST NODE.JS MOCK TEST I

Backend Development. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Backend Development. SWE 432, Fall Web Application Development

Web Application Development

What is Node.js? Tim Davis Director, The Turtle Partnership Ltd

Express.JS. Prof. Cesare Pautasso Modularity

Index. Backbone.js app, 274 Behavior-driven development (BDD) language, 252 bodyparser() method, 257

We are assuming you have node installed!

Chapter 1 - Development Setup of Angular

Introduction to Express.js. CSC309 Feb. 6, 2015 Surya Nallu

COMP 2406: Fundamentals of Web Applications. Winter 2014 Mid-Term Exam Solutions

Microservices with Node.js

Tools. SWE 432, Fall Design and Implementation of Software for the Web

SAMPLE CHAPTER SECOND EDITION. Alex Young Bradley Meck Mike Cantelon. Tim Oxley Marc Harter T.J. Holowaychuk Nathan Rajlich MANNING WITH

Smashing Node.JS: JavaScript Everywhere

Hands-on Lab Session 9011 Working with Node.js Apps in IBM Bluemix. Pam Geiger, Bluemix Enablement

Lets take a closer look at Ajax & node.js. Claudia Hauff TI1506: Web and Database Technology

Practical Node.js. Building Real-World Scalable Web Apps. Apress* Azat Mardan

Lab 1 - Introduction to Angular

Quick Desktop Application Development Using Electron

Angular 2 Programming

XMLHttpRequest. CS144: Web Applications

Bitnami Node.js for Huawei Enterprise Cloud

CS193X: Web Programming Fundamentals

CNIT 129S: Securing Web Applications. Ch 10: Attacking Back-End Components

Advance Mobile& Web Application development using Angular and Native Script

Brunch Documentation. Release Brunch team

Full Stack boot camp

MEAN Stack. 1. Introduction. 2. Foundation a. The Node.js framework b. Installing Node.js c. Using Node.js to execute scripts

Exercise 1. Bluemix and the Cloud Foundry command-line interface (CLI)

Persistence. SWE 432, Fall 2017 Design and Implementation of Software for the Web

NODE.JS SERVER SIDE JAVASCRIPT. Introduc)on Node.js

Running and Debugging Custom Components Locally

COMP 2406: Fundamentals of Web Applications Winter 2014 Final Exam Solutions Instructor: Anil Somayaji April 24, 2014

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Koa.js

Index. Bare-bones connect application, 125 Binary JSON (BSON), 171. Callback functions asynchronous code ifelse, 199 loops,

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Persistence & State. SWE 432, Fall 2016 Design and Implementation of Software for the Web

delegator Documentation

IERG 4210 Tutorial 08

Getting MEAN. with Mongo, Express, Angular, and Node SIMON HOLMES MANNING SHELTER ISLAND

Java vs JavaScript. For Enterprise Web Applications. Chris Bailey IBM Runtime Technologies IBM Corporation

Comprehensive AngularJS Programming (5 Days)

TypeScript. Types. CS144: Web Applications

MongoDB Web Architecture

Client Side JavaScript and AJAX

This program assumes you have basic knowledge or htaccess redirection.

Overview of BC Learning Network SMS2 Introduction

RequireJS Javascript Modules for the Browser. By Ben Keith Quoin, Inc.

This tutorial is meant for software developers who want to learn how to lose less time on API integrations!

5th April Installation Manual. Department of Computing and Networking Software Development Degree

IBM Image-Analysis Node.js

Module 6 Node.js and Socket.IO

Getting Started With NodeJS Feature Flags

INF5750. Introduction to JavaScript and Node.js

CGI Architecture Diagram. Web browser takes response from web server and displays either the received file or error message.

Getting started with Tabris.js Tutorial Ebook

CS193X: Web Programming Fundamentals

IN4MATX 133: User Interface Software

Homework #7 Amazon Elastic Compute Cloud Web Services

High Performance Single Page Application with Vue.js

Web Development for Dinosaurs An Introduction to Modern Web Development

welcome to BOILERCAMP HOW TO WEB DEV

Oracle Service Cloud Integration for Developers Ed 1

Setting up an Angular 4 MEAN Stack (Tutorial)

Databases/JQuery AUGUST 1, 2018

WorldSpace Attest Quick Start Guide

JSON Evaluation. User Store

Static Webpage Development

Advanced React JS + Redux Development

Guides SDL Server Documentation Document current as of 05/24/ :13 PM.

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Laravel

Node.js I Getting Started

APPLICATION INTERFACE

bst Documentation Release John Kelvie, Bela Vizy, Michael Myers

Extra Notes - Data Stores & APIs - using MongoDB and native driver

Frontend UI Training. Whats App :

#06 RPC & REST CLIENT/SERVER COMPUTING AND WEB TECHNOLOGIES

CS50 Quiz Review. November 13, 2017

LFE Medieninformatik WS 2016/2017

Copyright by Object Computing, Inc. (OCI). All rights reserved. Strata

Node.js. Mendel Rosenblum. CS142 Lecture Notes - Node.js

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. GraphQL

Indium Documentation. Release Nicolas Petton

Tools for Accessing REST APIs

Web Mechanisms. Draft: 2/23/13 6:54 PM 2013 Christopher Vickery

CSC 309 The Big Picture

Postman User Guide. Document Reference: July Version: 2

Aim behind client server architecture Characteristics of client and server Types of architectures

Tuesday, January 13, Backend III: Node.js with Databases

RESTful APIs ECS 189 WEB PROGRAMMING. Browser s view. Browser s view. Browser s view. Browser s view. Which will It be for photobooth?

Pemrograman Jaringan Web Client Access PTIIK

AWS Lambda + nodejs Hands-On Training

MASTERS COURSE IN FULL STACK WEB APPLICATION DEVELOPMENT W W W. W E B S T A C K A C A D E M Y. C O M

From the Beginning: Your First Node.js Web Service

get() method, 119, 190 Google s V8 JavaScript engine, 1

MongoDB. Robert M. Vunabandi

Transcription:

Node.js Node.js Overview JavaScript runtime environment based on Chrome V8 JavaScript engine Allows JavaScript to run on any computer JavaScript everywhere! On browsers and servers! Intended to run directly on OS, not inside a browser Removes browser-specific JavaScript API Adds support for OS APIs such as file system and network Cross-platform, runs on Linux, Windows, macos, Solaris,... Node package manager (NPM) to manage package dependency and installation Node.js is single threaded No overhead from multi-threading and leads to high-performance But requires asynchronous programming Node interactive JavaScript shell $ node > console. log (" Hello world "); Hello world undefined >. help. break Sometimes you get stuck, this gets you out. clear Alias for. break. editor Enter editor mode. exit Exit the repl. help Print this help message. load Load JS from a file into the REPL session. save Save all evaluated commands in this REPL session to a file >. exit Example: reply with Hello world to any HTTP request on port 3000 Junghoo "John" Cho (cho@cs.ucla.edu) 1

// ---------- app. js ---------- let http = require (" http "); // Create HTTP server and listen on port 3000 for r eq u e s t s http. createserver (( request, response ) => { response. writehead (200, { ' Content - Type ': ' text / plain ' response. write (" Hello World!\ n"); response. end (" URL : " + request. url ); }). listen (3000) ; $ node app. js Node.js is single-threaded Asynchronous-programming Passes a callback, which will be invoked when the server receives a request Nonblocking call More on this later Node.js everywhere! Node is not a server. It is a general JavaScript runtime platform Node.js is now used for desktop app development, not just for servers Example: Electron, AppJS,... Node package manager (NPM) Package manager to help installing and managing Node.js packages (or modules) $ npm install express Install package locally in the node_modules/ subdirectory package.json: a file created in the app root folder to describe package dependency $ npm init -y $ npm install -- save express Junghoo "John" Cho (cho@cs.ucla.edu) 2

npm init -y creates default package.json --save option adds the installed package to package.json // --- package. json ---- { " name ": " application - name ", " version ": " 0.0.0 ", " private ": true, " scripts ": { " start ": " node./ bin / www " }, " dependencies ": { " express ": " 4.9.0 ", " cookie - parser ": " ~1.3.3 ", " ejs ": " ^1.6.0 " } } Version: major.minor.patch ~ any patch version is fine ^ an equal or higher version is fine if the major version is the same npm install installs all dependencies listed in the file in node_modules Global mode $ npm install - g nodemon $ nodemon app. js Package nodemon supports automatic restart of node after code change Global vs local? If a package is used in the program, using require('whatever'), install it locally If a package is used in the shell or command line during development, install it globally Express (express) A popular node framework for Web server development Junghoo "John" Cho (cho@cs.ucla.edu) 3

Provides URL-routing mechanism Integrates HTML template engine Integrates middleware for complex request handling First example // -------- app. js --------- let express = require ( ' express '); let app = express () ; app. get ( '/ ', ( req, res ) => { res. send ( ' Hello World! '); app. get ( '/ john ', ( req, res ) => { res. send ( ' Hello, John! '); app. listen (3000) ; $ node app. js app.get(path, callback) maps GET request at path to callback function res.send(body) returns body as the response to the request app.listen(port) binds to port Q: What does the above server do? What URLs does it handle? What response does it generate? Q: Should I install express locally or globally? $ npm init -y $ node install -- save express Junghoo "John" Cho (cho@cs.ucla.edu) 4

Figure 1: Express-MVC Express supports MVC pattern Q: How do I map a path to a controller/function? Routing support express.method(path, callback) Invoke callback for a request with method and path Path interprets?, +,, and () as regular expression operators Exact syntax at https://www.npmjs.com/package/path-to-regexp Example app. get ( '/ list /: username ', ( req, res ) => { res. send (" Your username is " + req. params. username ); Inside callback, the HTTP request is available in the first parameter req: req.query: query strings in the URL req.params: /:xxx parameters in the URL req.body: request body req.cookies: cookies If a request does not have any matching path, express responds with status 404 Not Found Junghoo "John" Cho (cho@cs.ucla.edu) 5

View generation Response code: response.status(200) Body: send(), sendfile(), render(), json() Raw string: response.send(body) Send the string body as the response Example: res.send("hello, world!") Static file: response.sendfile(absolute_path_to_file) Send a static file from local filesystem Example: res.sendfile('/user/cho/public/index.html') JSON: response.json(object) Encode JavaScript object object in JSON Example: res.json({title: "Hello", body: "_Love_"}) HTML from template: response.render(template-file, template-data) Generate an HTML page from template-file using template-data Template engine: different template engines may be used Examples: Pug (previously Jade), EJS, Mustache,... Example: res.render('index', {title: "Hello"}) Redirect: response.redirect(url) Send redirect response Example: res.redirect('/') EJS: Example template engine (ejs) A popular template engine used with Express Scriptlet tag <%... %>: javascript for control-flow. no output <%=... %>: print out the result of expression (after HTML escaping) <%-... %>: print out the result (raw output. No HTML escaping) Example // ----- index. ejs ------ <! DOCTYPE html > < html < head >< title ><%= title % ></ title ></ head > < body > Junghoo "John" Cho (cho@cs.ucla.edu) 6

<ul > <% for ( let post of posts ) { % > <li ><%= post. title % ></ li > <% } % > </ ul > </ body > // ---- app. js ------ let express = require ( ' express '); let app = express () ; app. set ( ' view engine ', ' ejs '); app. set ( ' views ', '. ') app. get ( '/ ', ( req, res ) => { res. render ( ' index ', { title : " Hello ", posts : [{ title : " Title 1"}, { title : " Title 2" }] } ); app. listen (3000) ; To run the above code, we need to install ejs module: npm install --save ejs Middleware integration express.use([path,] middleware) attaches a middleware to the request handling chain Middleware may take an action based on the request, transform the request, serve the response to the request, etc Every request goes through the sequence of middleware, until one serves the response If path is specified, the middleware runs only on the request within path Example Junghoo "John" Cho (cho@cs.ucla.edu) 7

let express = require ( ' express '); let path = require ( ' path '); let bodyparser = require ( 'body - parser '); let app = express () ; app. use ( bodyparser. json () ); app. use ( '/ www / ', express. static ( path. join ( dirname, ' public ') )); app. get ( '/ ', ( req, res ) => { res. send (" Welcome to our Application!"); }) app. listen (3000) ; express.static(absolute_path_to_root_dir) Middleware for serving static files from a directory Multiple static asset directories can be specified for the same path. The first match is returned bodyparser collection of HTTP body parsing middleware Creating middleware express.router(): creates a modular, mountable route handler inside a middleware Using router.method(path, callback), we can specify further route mapping inside a middleware Mini app: a router can be used like a middleware and can be passed as a parameter to express.use() Example: // ---- app. js -------- let express = require ( ' express '); let birds = require ( './ birds '); let app = express () ; Junghoo "John" Cho (cho@cs.ucla.edu) 8

app. use ( '/ birds ', birds ); app. get ( '/ ', ( req, res ) => { res. send (" Welcome to Birds App!"); }) app. listen (3000) ; // ---- birds. js ------- let express = require ( ' express '); let router = express. Router () ; router. get ( '/ ', function ( req, res ) { res. send ( ' Birds home page '); }) router. get ( '/ about ', function ( req, res ) { }) res. send ( ' About birds '); module. exports = router ; Q: What will be returned for /birds? /birds/about? Express application generator Express application generator can be used to generate a skeleton code for expressbased server $ express -e // generate skeleton code with EJS template engine $ npm install // install dependent modules $ npm start // execute " start " script in package. json $ ls app. js package - lock. json routes / bin / package. json t. js node_modules / public / views / Junghoo "John" Cho (cho@cs.ucla.edu) 9

app.js: main application public/: folder for static files routes/: route handlers views/: views // app. js ---- var index = require ( './ routes / index '); var users = require ( './ routes / users '); var app = express () ; // middleware app. use ( logger ( ' dev ')); app. use ( bodyparser. json () ); app. use ( bodyparser. urlencoded ({ extended : false }) ); app. use ( cookieparser () ); app. use ( express. static ( path. join ( dirname, ' public '))); app. use ( '/ ', index ); app. use ( '/ users ', users ); // catch 404 and forward to error handler app. use ( function ( req, res, next ) { var err = new Error ( ' Not Found '); err. status = 404; next ( err ); // routes / index. js var express = require ( ' express '); var router = express. Router () ; / GET home page. / router. get ( '/ ', function ( req, res, next ) { res. render ( ' index ', { title : ' Express ' Junghoo "John" Cho (cho@cs.ucla.edu) 10

module. exports = router ; Junghoo "John" Cho (cho@cs.ucla.edu) 11