Chengyu Sun California State University, Los Angeles

Slides:



Advertisements
Similar presentations
Java Script Session1 INTRODUCTION.
Advertisements

© 2014 IBM Corporation Empowering the IBM ecosystem Introduction to Developing applications using node.js on Bluemix IBM Ecosystem Development Instructors.
DT211/3 Internet Application Development
DT228/3 Web Development JSP: Directives and Scripting elements.
Chapter 11 ASP.NET JavaScript, Third Edition. 2 Objectives Learn about client/server architecture Study server-side scripting Create ASP.NET applications.
PHP Scripting Language. Introduction “PHP” is an acronym for “PHP: Hypertext Preprocessor.” It is an interpreted, server-side scripting language. Originally.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Server- Side technologies Client-side vs. Server-side scripts PHP basic ASP.NET basic ColdFusion.
ASP.NET 5 Visual Studio 2015 Templates Bill Wolff Rob Keiser June 10, 2015.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4.
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
IS-907 Java EE World Wide Web - Overview. World Wide Web - History Tim Berners-Lee, CERN, 1990 Enable researchers to share information: Remote Access.
Securing Angular Apps Brian Noyes
 Web pages originally static  Page is delivered exactly as stored on server  Same information displayed for all users, from all contexts  Dynamic.
Presentation Title Subtitle DSpace UI Prototype 7 Spring, Angular.js, and the DSpace REST API.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
Node.JS introduction. What is Node.JS? v8 JavaScript runtime Event driven Non-blocking standard libraries Most APIs speak streams Provides a package manager.
CS520 Web Programming Bits and Pieces of Web Programming (II) Chengyu Sun California State University, Los Angeles.
Introduction to Node.js® Jitendra Kumar Patel Saturday, January 31, 2015.
//liveVirtualacademy2011/ What’s New for ASP.NET 4.5 and Web Development in Visual Studio 11 Developer Preview Γιώργος Καπνιάς MVP, MCT, MCDP, MCDBA, MCTS,
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
Node.js Modules Header Mastering Node.js, Part 2 Eric W. Greene
NodeJS and MEAN cs6320.
JSP: Actions elements and JSTL
Introduction to Dynamic Web Programming
Web-Technology Lecture 11.
Node.js Express Web Applications
ASP MVP Web applications and Razor
Creating UI elements with Handlebars
Node.js Express Web Services
Recitation – Week 8 Pranut jain.
NodeJS and MEAN Prof. L. Grewe.
CMPE 280 Web UI Design and Development October 19 Class Meeting
3 Things Everyone Knows About Node JS That You Don't
CMPE 280 Web UI Design and Development October 24 Class Meeting
JavaScript: ExpressJS Overview
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Course Overview
Network Programming Lecture 4: Message Posting
JavaScript an introduction.
Web Systems Development (CSC-215)
CMPE 280 Web UI Design and Development January 30 Class Meeting
CS5220 Advanced Topics in Web Programming Spring – Web MVC
Nathan Totten Technical Evangelist Windows Azure
CS5220 Advanced Topics in Web Programming JavaScript Basics
Secure Web Programming
CMPE 280 Web UI Design and Development January 29 Class Meeting
Chengyu Sun California State University, Los Angeles
RESTful Web Services.
CS5220 Advanced Topics in Web Programming More Node.js
CS5220 Advanced Topics in Web Programming Angular – Services
CS5220 Advanced Topics in Web Programming Secure REST API
An Introduction to JavaScript
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Course Overview
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Angular – Services
CS5220 Advanced Topics in Web Programming More Node.js
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
MEAN login management CS252.
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
CS4540 Special Topics in Web Development Course Overview
CMPE 280 Web UI Design and Development August 27 Class Meeting
Presentation transcript:

Chengyu Sun California State University, Los Angeles CS5220 Advanced Topics in Web Programming Develop Web Applications and Services Using Express Chengyu Sun California State University, Los Angeles

Using Express Application Generator Install global package express-generator express command options -h, --help -v, --view -c, --css --git

Template Engine A template engine combines templates with data to produce documents A.K.A. view engine, though it’s not just for views in MVC, e.g. email templates

JSP As A Template Engine <p>Hello, ${user.firstName}!</p> <c:if test=“${not empty tasks}”> <p>Your tasks for today:</p> <ul> <c:forEach items=“${tasks}” var=“task”> <li>${task}</li> </c:forEach> </ul> </c:if>

Basic Characteristics of a Template Engine Template language Access objects and properties Basic expression and control flow statements Server and/or client-side rendering Support for formatting and i18n Performance

Hogan <p>Hello, {{user.firstName}}!</p> {{#hasTasks}} <p>Your tasks for today:</p> <ul> {{#tasks}} <li>{{.}}</li> {{/tasks}} </ul> {{/hasTasks}} Developed by Twitter Use the “logic-less” Mustache template language

Handlebars <p>Hello, {{user.firstName}}!</p> {{#if tasks}} <p>Your tasks for today:</p> <ul> {{#each tasks}} <li>{{.}}</li> {{/each}} </ul> {{/if}} Extension to Mustache (arguably making it easier to use)

Dust Developed by LinkedIn <p>Hello, {user.firstName}!</p> {?tasks} <p>Your tasks for today:</p> <ul> {#tasks} <li>{.}</li> {/tasks} </ul> Developed by LinkedIn

EJS <p>Hello, <%= user.firstName %>!</p> <% if (tasks) { %> <p>Your tasks for today:</p> <ul> <% for( var i=0 ; i < tasks.length ; ++i ) { %> <li><%= tasks[i] %></li> <% } %> </ul> Syntax is similar to JSP scripting elements

Twig <p>Hello, {{user.firstName}}!</p> {% if tasks %} <p>Your tasks for today:</p> <ul> {% for task in tasks %} <li>{{task}}</li> {% endfor %} </ul> {% endif %} Originally a PHP template engine

Vash <p>Hello, @user.firstName!</p> if( tasks ){ <p>Your tasks for today:</p> <ul> @tasks.forEach( function(task) { <li>@task</li> } </ul> Use the Razor (i.e. the view engine in ASP.NET MVC) syntax

Jade / Pug p Hello, #{user.firstName}! if tasks p Your tasks for today: ul each task in tasks li= task Jade is renamed to Pug due to trademark problem

Build A MVC Web Application Understand the application structure created by Express Generator Data models and database access Controllers and views

Application Structure /public for static resources /routes for controllers /views for view templates /bin for executables package.json Packages npm start

Customize Configuration Using Environment Variables process.env.PORT || '3000' We often need to change runtime configuration such as port number, database url/username/password, log file location and so on Java applications usually use property files Node.js application prefer environment variables

Set Environment Variables Using dotenv Package Put the variables in a .env file, e.g. DBURL=mongodb://localhost/webtest USERNAME=cysun PASSWORD=abcd Run require('dotenv').config() at the beginning of the application Include and version control a .env.sample file

Model and DB Add Mongoose models Connect to database mongoose.connect(<DBURL>) Mongoose automatically creates a connection pool Disconnect during shutdown

Graceful Shutdown async function shutdown(callback) { await mongoose.disconnect(); if (callback) callback(); else process.exit(0); } process.on('SIGINT', shutdown); process.on('SIGTERM', shutdown); process.once('SIGUSR2', () => { shutdown(() => process.kill(process.pid, 'SIGUSR2')); }); Why didn’t we “await” on mongoose.connect()?

(model attributes in Spring) Controller router.get('/users', function(req, res, next) { User.find( (err, users) => { if(err) return next(err); res.render('users’, {title: 'Users', users: users}); }); Handle Error Render View View Name “Locals” (model attributes in Spring)

Handlebars Views layout.hbs is the default master page Set a layout local to use a different master page A child view is combined with the master page as {{{body}}} Triple braces mean do not escape content

Implement REST API Mostly it's just MVC without View Authentication and authorization with JWT Testing with Jasmine

Login Endpoint Use jsonwebtoken to create JWT jwt = require('jsonwebtoken'); jwt.sign( <payload>, <secret> [, options] ); Usually a User object Common claims such as sub and exp are in options

JWT Authorization Using Middleware Extract JWT token from Authorization header Verify the signature Attach the payload (i.e. the user object) to req and pass it onto the next middleware Return 401 if anything is wrong

Passport Passport is a popular Node.js authentication framework Different authentication schemes (called strategies) can be implemented on top of Passport

Using Passport-JWT … Example: passport-jwt.js Registers JWT strategy with Passport Extract JWT from Authorization header as a bearer token (other extractions also possible) Decode and verify token Passes decoded payload and a done() function to the callback function

… Using Passport-JWT … The callback function allows further processing of the payload, e.g. loading a full user object from database done(err, user) sets req.user which can be used by subsequent middleware

… Using Passport-JWT Add Passport to an Express application app.use( passport.initialize() ); app.use( '/api/', passport.authenticate('jwt', { session: false, failWithError: true }));

Jasmine Test Framework An all-in-one test framework for JavaScript Default test framework used by Angular

Using Jasmine npm install --save-dev jasmine ./node_modules/.bin/jasmine init Set jasmine as test script in package.json npm test

Test Example Test suite: describe Test spec: it beforeAll(), afterAll(), beforeEach(), afterEach() done() informs Jasmine that an asynchronous function has finished Test spec: it Expectations and matchers