Express.JS. Prof. Cesare Pautasso Modularity

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

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

MongoDB Web Architecture

BIS Database Management Systems.

MIS Database Systems.

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

About 1. Chapter 1: Getting started with mongoose 2. Remarks 2. Versions 2. Examples 4. Installation 4. Connecting to MongoDB database: 4

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

IERG Tutuorial 5. Benedict Mak

Web Application Development

Backend Development. SWE 432, Fall Web Application Development

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

Mike Cantelon Marc Harter T.J. Holowaychuk Nathan Rajlich. FOREWORD BY Isaac Z. Schlueter MANNING

Review. Fundamentals of Website Development. Web Extensions Server side & Where is your JOB? The Department of Computer Science 11/30/2015

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

MEAN February. techdt.la

Catbook Workshop: Intro to NodeJS. Monde Duinkharjav

Future Web App Technologies

Microservices with Node.js

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

MongoDB. Robert M. Vunabandi

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

Front End Programming

Storage Tier. Mendel Rosenblum. CS142 Lecture Notes - Database.js

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

Topic 12: Connecting Express and Mongo

Security. CSC309 TA: Sukwon Oh

Hybrid mobile application with Ionic and MEAN stack

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

Topics. History. Architecture. MongoDB, Mongoose - RDBMS - SQL. - NoSQL

a Very Short Introduction to AngularJS

JSON Evaluation. User Store

We are assuming you have node installed!

Web Application Development

AWS Lambda + nodejs Hands-On Training

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

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

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

MongoDB. CSC309 TA: Sukwon Oh

Symfony is based on the classic web design pattern called the MVC pattern

Online. Course Packet PYTHON MEAN.NET

COSC 416 NoSQL Databases. NoSQL Databases Overview. Dr. Ramon Lawrence University of British Columbia Okanagan

welcome to BOILERCAMP HOW TO WEB DEV

Full Stack boot camp

Bitnami Node.js for Huawei Enterprise Cloud

Getting Mean. Practical Perspective. Prof. Dr. Alejandro Zunino, Prof. Dr. Alfredo Teyseyre. ISISTAN Department of Computer Science UNICEN

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

Outline. 1. Load Testing 2. Frontend Tips 3. Server-side Tips

Scaling DreamFactory

Simplifying Web Application Development Using - Mean Stack Technologies

NoSQL & Firebase. SWE 432, Fall Web Application Development

COSC 304 Introduction to Database Systems. NoSQL Databases. Dr. Ramon Lawrence University of British Columbia Okanagan

Web Applications. Software Engineering 2017 Alessio Gambi - Saarland University

Introduction to NoSQL Databases

Petr CZJUG, December 2010

CIB Session 12th NoSQL Databases Structures

Real Time Marketing and Sales Data

Online Multimedia Winter semester 2015/16

This tutorial helps the professionals aspiring to make a career in Big Data and NoSQL databases, especially the documents store.

LFE Medieninformatik WS 2016/2017

Advance Mobile& Web Application development using Angular and Native Script

Enterprise Systems & Frameworks

ThingWorx Relational Databases Connectors Extension User Guide

Presented by Sunnie S Chung CIS 612

Big Data Technology Ecosystem. Mark Burnette Pentaho Director Sales Engineering, Hitachi Vantara

Databases/JQuery AUGUST 1, 2018

The NoSQL Ecosystem. Adam Marcus MIT CSAIL

Chapter 24 NOSQL Databases and Big Data Storage Systems

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

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

Running and Debugging Custom Components Locally

Overview. * Some History. * What is NoSQL? * Why NoSQL? * RDBMS vs NoSQL. * NoSQL Taxonomy. *TowardsNewSQL

Oracle APEX 18.1 New Features

MAKE NODEJS APIs GREAT WITH TYPESCRIPT

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

CS193X: Web Programming Fundamentals

From the Beginning: Your First Node.js Web Service

Database Solution in Cloud Computing

Developer Internship Opportunity at I-CC

Part I What are Databases?

IN ACTION Writing, building, and testing Node.js applications

NoSQL Databases MongoDB vs Cassandra. Kenny Huynh, Andre Chik, Kevin Vu

CSC 309 The Big Picture

Goal of the presentation is to give an introduction of NoSQL databases, why they are there.

DATABASES SQL INFOTEK SOLUTIONS TEAM

Databases : Lecture 1 2: Beyond ACID/Relational databases Timothy G. Griffin Lent Term Apologies to Martin Fowler ( NoSQL Distilled )

Course: Database Management Systems. Lê Thị Bảo Thu

Introduction to Relational Databases

PHP WITH ANGULAR CURRICULUM. What you will Be Able to Achieve During This Course

Introduction to Graph Databases

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

Stages of Data Processing


Notes. Submit homework on Blackboard The first homework deadline is the end of Sunday, Feb 11 th. Final slides have 'Spring 2018' in chapter title

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

Which compute option is designed for the above scenario? A. OpenWhisk B. Containers C. Virtual Servers D. Cloud Foundry

What is a Database? Peter Wood

CS193X: Web Programming Fundamentals

What is database? Types and Examples

Mysql Query Browser Export Table Structure

Transcription:

1 / 30 Express.JS Prof. Cesare Pautasso http://www.pautasso.info cesare.pautasso@usi.ch @pautasso Modularity var msg = "x:"; //private var f = function(x) { return msg + " " + x; module.exports.f = f; //public./my_module.js 2 / 30 Export module functions making them part of module.exports var module = require('module'); //library module var my_module = require('./my_module'); //local module console.log(my_module.f("hello")); Import modules with require 2013 Cesare Pautasso

2013 Cesare Pautasso package.json 3 / 30 { "name": "my-app", "description": "My Web Application", "version": "0.0.1", "dependencies": { "express": "3.x"./package.json dependencies lists the required modules and their version Use the Node Package Manager (npm) to: npm init interactively create a new package.json npm install download and install the dependencies

Model View Controller 4 / 30 Controllers Model Views Input Output User input is handled by the Controller which may change the state of the Model The Views display the current state of the Model (and may receive notifications about state changes by observing it) MVC on the Web 1.0 5 / 30 Request Response Controllers Views Model In Web applications, views produce the actual HTML pages sent to the browser. The controller is the code that handles the input requests, reads/writes the data model and invokes the view to format the model in HTML (or other) representation. The data model manages the actual content and the state of the application, usually stored persistently in a database or XML files.

MVC on the Web 2.0 6 / 30 Web Browser Web Server Request Controllers DOM View Response JSON Output Model The Web server responds with JSON data extracted from the model The view running on the client renders the JSON into HTML/DOM Express MVC 7 / 30 Your Views Your Controllers MVC Framework (express.js) JavaScript Your Model Web Server Database

Express MVC 8 / 30 var express = require('express'); var app = express(); app.get('/', function(req, res) { db.findall(function(error, model) { res.render('view', model); ) app.listen(8888); Routing 9 / 30 app.method('uri Template', callback) METHOD = {get, put, post, delete, all app.get('/user/:id', function(req, res){ res.send('user ' + req.params.id); app.get('/file/*.*', function(req, res){ res.send('file path: ' + req.params); app.routes //read the configuration

Parse the Request Body 10 / 30 app.use(express.bodyparser()); app.put('/blog/:year/:id?', //? optional function(req, res){ var year = req.params.year; var id = req.params.id 0; //default 0 var body = req.body;... var files = req.files; //uploaded files Content-Type 11 / 30 //Request Content-Type:application/json if (req.is("application/json")) { //req body is JSON //Request Accept:text/html if (req.accepts("html")) { //set Response Content-Type Header res.type("text/html");

Views 12 / 30 function(model) { return html Views are template files that present content to the user: variables, arrays and objects that are used in views are passed by the controller to be rendered by the view. Views should not contain complex business logic; only the elementary control structures necessary to perform particular operations, such as the iteration over collections, and simple filters. Views (Manual) 13 / 30 function(model) { var html = []; html.push('<html><body>'); if (model.user) { html.push('<h2>' + model.user.name + '</h2>'); html.push('</body></html>'); return html.join();

Views (Rendered) 14 / 30 res.render('view template', { model ); Invoke a template to render the view //render the index template using JADE res.render('index.jade'); //render the index template using EJS res.render('index.ejs'); app.set('view engine', 'ejs'); res.render('index'); Embedded JS Templates 15 / 30 var model = { user: { name: 'CP' ; res.render('index.ejs', model ); app.js <html><body> <% if (user) { %> <h2><%=user.name %></h2> <% %> </body></html> <html><body> <h2>cp</h2> </body></html> index.ejs Result

Embedded JS Templates 16 / 30 <h1><%= title %></h1> <ul> <% for(var i=0; i<list.length; i++) {%> <li><%= link_to(list[i], users/'+list[i]) %></li> <% %> </ul>.ejs JavaScript between <% %> is executed JavaScript between <%= %> adds the result to the HTML View Helpers 17 / 30 Reduce the amount of code by using these helper functions (mini-templates or widgets) date_tag form_tag form_tag_end hidden_field_tag input_field_tag is_current_page link_to submit_link_to link_to_if link_to_unless password_field_tag select_tag submit_tag text_area_tag text_field_tag img_tag http://code.google.com/p/embeddedjavascript/wiki/viewhelpers

Databases 18 / 30 Manage the persistent storage of structured information Simplify writing of programs to query the information (selection, filtering, sorting, aggregation, translation) with declarative query languages Guarantee consistency of the data as it is modified with concurrent transactions Guarantee reliability of the data thanks to advanced crash recovery features Very often used to implement the model of MVC Web applications on the server Working with Databases 19 / 30 Your Application Objects Queries Transactions Schema (metadata) Database Server Tables (data)

SQL 20 / 30 Relational Databases Tables (Relations) store well-structured data according to a predefined Schema Queries defined according to the standard Structured Query Language Strong transactional guarantees (ACID) NoSQL Non-relational Databases (Object-oriented databases, document databases, graph databases, key-value stores, XML databases) Schema-less (semi-structured heterogeneous data stores) Highly scalable with relaxed transactional guarantees and limited query expressivity Examples SQL 21 / 30 mysql -- PostgreSQL -- Oracle -- Microsoft SQL Server - - IBM DB2 NoSQL CouchDB -- MongoDB -- Apache JackRabbit -- Neo4j - - MemCacheDB -- BigTable -- Apache Hadoop

MongoDB 22 / 30 Database Collection Collection Collection A collection is a set of JSON objects, which may or may not have the same structure 1. Connecting 23 / 30 var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/db'); // The MongoDB Server should be running // for the connection to work

2. Schema Definition 24 / 30 var CommentsSchema = new mongoose.schema ({ person : String, comment : String, created_at : Date 2. Schema Definition 25 / 30 var PostSchema = new mongoose.schema({ author : mongoose.objectid, title : String, body : String, created_at : Date, comments : [CommentsSchema] //Create the Collection Post using Schema mongoose.model('post', PostSchema); var Post = mongoose.model('post');

3. Queries 26 / 30 Post.find({, function (err, posts) { //for each post in posts Post.findById(id, function (err, post) { if (!err) { //found post with id 4. Create and Save 27 / 30 var post = new Post( { title: params['title'], body: params['body'], created_at: new Date() Save also works with updates post.save(function (err) { //save failed 5. Delete 28 / 30 Post.findById(id, function (err, post) { if (!err) { post.remove();

Tools 29 / 30 Express (http://expressjs.com/) framework) Node.JS (http://nodejs.org/) Embedded JS (View Templating Engine) MongoDB Driver) Database) (Node.js (http://code.google.com/p/embeddedjavascript/) Mongoose (http://mongoosejs.com/) MongoDB (http://www.mongodb.org/) (Node- (JSON