IERG Tutuorial 5. Benedict Mak

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

CSc 337 LECTURE 16: WRITING YOUR OWN WEB SERVICE

We are assuming you have node installed!

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

Express.JS. Prof. Cesare Pautasso Modularity

Part 1: IoT demo Part 2: MySQL, JSON and Flexible storage

IERG 4210 Tutorial 07. Securing web page (I): login page and admin user authentication Shizhan Zhu

IERG 4210 Tutorial 08

From the Beginning: Your First Node.js Web Service

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

NoSQL & Firebase. SWE 432, Fall Web Application Development

CS193X: Web Programming Fundamentals

Client Side JavaScript and AJAX

Using PHP with MYSQL

Data Analysis and Integration

crane Documentation Release Globo.com

Lecture 10(-ish) Web [Application] Frameworks

Bitnami MariaDB for Huawei Enterprise Cloud

NODE HERO Get started with Node.js and deliver software products using it. From the Engineers of. RisingStack 1

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

Bitnami MySQL for Huawei Enterprise Cloud

Bitnami MEAN for Huawei Enterprise Cloud

Backend Development. SWE 432, Fall Web Application Development

Bitnami Node.js for Huawei Enterprise Cloud

PHPRad. PHPRad At a Glance. This tutorial will show you basic functionalities in PHPRad and

IEMS 5722 Mobile Network Programming and Distributed Server Architecture Semester 2

Web API Lab. The next two deliverables you shall write yourself.

Download and install MySQL server 8 in Windows. Step1: Download windows installer

Databases/JQuery AUGUST 1, 2018

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

PHP: Cookies, Sessions, Databases. CS174. Chris Pollett. Sep 24, 2008.

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

Mysql Tutorial Show Table Like Name Not >>>CLICK HERE<<<

CSC 215 PROJECT 2 DR. GODFREY C. MUGANDA

CS193X: Web Programming Fundamentals

Hydra Installation Manual

A Simple Course Management Website

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

Cookies, sessions and authentication

Apache Install Instructions Win7 7 Php Mysql. Phpmyadmin Linux >>>CLICK HERE<<<

Administration Dashboard Installation Guide SQream Technologies

Mysql Information Schema Update Time Null >>>CLICK HERE<<< doctrine:schema:update --dump-sql ALTER TABLE categorie

Creating a Yubikey MFA Service in AWS

Security and Privacy. SWE 432, Fall 2016 Design and Implementation of Software for the Web

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

Developing ASP.Net MVC 4 Web Application

postgresql postgresql in Flask in 15 or so slides 37 slides

Configuration Setting

Linux Network Administration. MySQL COMP1071 Summer 2017

PHP. MIT 6.470, IAP 2010 Yafim Landa

Catbook Workshop: Intro to NodeJS. Monde Duinkharjav

IELM 511 Information Systems Design Labs 5 and 6. DB creation and Population

Creating the Data Layer

COURSE 20486B: DEVELOPING ASP.NET MVC 4 WEB APPLICATIONS

Web Application Development

WEB SECURITY WORKSHOP TEXSAW Presented by Solomon Boyd and Jiayang Wang

Bugspad Documentation

Defining New Node-RED Nodes

The connection has timed out

Installing PHP on Windows 10 Bash and Starting a Local Server

Kollaborate Server. Installation Guide

Making Sling Grunt Or How to Integrate Modern Front-End Development with Sling. Philip Hornig (Publicis Pixelpark), Michael Sunaric (Netcentric)

Developing ASP.NET MVC 4 Web Applications

Unifer Documentation. Release V1.0. Matthew S

LFE Medieninformatik WS 2016/2017

Getting Started with

L.A.M.P. Stack Part I

UNIT 9 Introduction to Linux and Ubuntu

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

XML JavaScript Object Notation JSON Cookies Miscellaneous What Javascript can t do. OOP Concepts of JS

Hi I m Jamund and I love ES6 and I m giving this talk.

Full Stack Web Developer

Buzztouch Server 2.0 with Amazon EC2

August, HPE Propel Microservices & Jumpstart

Database and MySQL Temasek Polytechnic

Install latest version of Roundcube (Webmail) on CentOS 7

Brunch Documentation. Release Brunch team

Contents in Detail. Foreword by Xavier Noria

PiranaJS installation guide

Installation of Gnet on Linux and Unix

Jarvis Web Gateway. Installation Instructions. Jonathan Couper-Smartt

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

Developing ASP.NET MVC 5 Web Applications. Course Outline

CS 2316 Homework 9a Login Due: Friday, November 2nd, before 11:55 PM Out of 100 points. Premise

Getting Started with IBM Bluemix Hands-On Workshop. Module 6a: Services

RailsConf Europe 2008 Juggernaut Realtime Rails. Alex MacCaw and Stuart Eccles

Writing Secure Chrome Apps and Extensions

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

Installing MediaWiki using VirtualBox

Lecture 7: Web hacking 3, SQL injection, Xpath injection, Server side template injection, File inclusion

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

Installing SeisComP3

Elegans Documentation

Using MySQL on the Winthrop Linux Systems

20486: Developing ASP.NET MVC 4 Web Applications (5 Days)

Installing LAMP on Ubuntu and (Lucid Lynx, Maverick Meerkat)

Full Stack Web Developer

Configuring Anonymous Access to Analysis Files in TIBCO Spotfire 7.5

Database Username And Current User Schema Do Not Match Sql Server

Real Application Security Administration

Transcription:

IERG4210 - Tutuorial 5 Benedict Mak

Handlebars - Basic - Handlebars - Three elements - Template, control JS, Data - Two ways to use Handlebars - Client side - Handlebars - Get data in the form of JSON from server - Populate the page with data according to the template - Server side - Express Handlebars - Get data directly from SQL server - Render the page with data according to the template and send to user

Handlebars - Basic (Client side) - Handlebars can run without express - One template page <script id="entry-template" type="text/x-handlebars-template"> <h1>{{title}}</h1> {{body}} </script> - One Control JS (include handlebars and jquery library) var source = $("#entry-template").html(); var template = Handlebars.compile(source); var data = {title: "My New Post", body: "This is my first post!"}; $('body').html(template(data)); http://jsfiddle.net/aytleryb/

Handlebars - Basic (Server side) - Let s talk about handlebars in Nodejs - Install express-handlebars npm install express3-handlebars --save - Create the folder hierarchy./views/layout - The default folder where handlebars look for your template page (extension.handlebars) - Can change later

Handlebars - Basic (Server side) - In app.js var express = require('express'), exphbs = require('express3-handlebars'), app = express(); app.engine('hbs', exphbs({defaultlayout:'main', layoutsdir: 'views/', extname: '.hbs'})); app.set('view engine', 'hbs'); app.get('/', function (req, res) { res.render('home', {layout: 'main' app.listen(80); http://runnable.com/vnd4czx21r1szdmv/express-handlebars-demo-for-node-js

Handlebars - Basic (Server side) - app.engine('hbs', exphbs({defaultlayout: main, layoutsdir: 'views/', extname:.hbs })); - defaultlayout: main - state what is your default layout - app.engine ( hbs ) - need to consistent with your extension name - state where you store the layouts, and what is the extension name - Optional, default is views/layouts/,.handlebars - app.get('/', function (req, res) { res.render('home', {layout: 'main' - / - when user request this, execute the function - res.render( home ) - render file home with the layout - layout: main - Optional, state what is your layout file, override defaultlayout

Handlebars - Basic (Server side) In views/main.hbs <html> {{{body}}} </html> {{{body}}} - placeholder for the main content to be rendered {{{}}} - Don t escape the value inside those parentheses Escape - turn < to < In views/home.hbs Some Content http://runnable.com/vnd4czx21r1szdmv/express-handlebars-demo-for-node-js

Handlebars - Data - Lets talk about how to use handlebars with data - In the template/view page - Client side - the template html - Server side - home.hbs in our example - use {{ dataname }} as a placeholder for data named as dataname - In JS - (Client Side) var data = {dataname: "My New Post"}; $('body').html(template(data)); - (Server Side) app.get('/', function (req, res) { var data = {dataname: "My New Post"}; res.render('home', data);

Handlebars - Helpers - Help you to do something more in the template - {{#if}} - If the condition is true, display the thing in the middle. - {{#if condition}} Display {{dataname}} {{/if} - {{#each}} - {{#each items}} {{name}} {{emotion}} {{/each}} - Iterate the the rows in items var data = { items: [ ] {name: "Handlebars", emotion: "love"}, {name: "Mustache", emotion: "enjoy"}, {name: "Ember", emotion: "want to learn"}

Handlebars - Helpers - You can register your own helper - Client Side Handlebars.registerHelper('fullName', function(person) { return person.firstname + " " + person.lastname; - Server Side app.get('/', function (req, res, next) { res.render('home', { } helpers: { foo: function () { return 'foo.'; }

Handlebars - Helpers - Define own helper seems very useful - Avoid do strings concatenation (e.g. person.firstnam " + person.lastname) - Hard for developers to apply output filtering - Also, dont use Handlebars.SafeString() to avoid HTML escape, very unsafe

Assignment 3a Hints - Use mysql - Install mysql - sudo apt-get install mysql-server (ubuntu) - yum install mysql-server mysql (red hat / amazon linux) - Install mysql driver for nodejs - npm install mysql --save - Login to mysql - mysql -u root -p - Creata database and use - create database shop00; - use shop00;

Assignment 3a Hints - Create table CREATE TABLE categories ( catid INT(6) UNSIGNED unique PRIMARY KEY, name VARCHAR(30) NOT NULL ); - There s a lot of other data types. Check it out. - Security - Create another user, tight privilege is better, block access from other IPs except localhost - SELECT User, Host, Password FROM mysql.user; - It shows all the users and related host, delete all the non-localhost entries - CREATE USER 'dummy'@'localhost' IDENTIFIED BY 'mypass'; - GRANT insert, select, update, delete ON shop00.* TO 'dummy'@'localhost'; - Grant no administrative privilege - After select which item to change, display all the sub-elements of that item first

var express = require('express'), app = express(), mysql = require('mysql'), connectionpool = mysql.createpool({ host user : 'localhost', : 'root', password : '1234', database : 'shop00' app.get('/table', function(req,res){ connectionpool.getconnection(function (err, connection) { if (!err) { connection.query('select * from categories', function(err, rows) { } if (!err) { res.json(rows); } app.listen(8080); connection.release(); This code will return a json response, pass the json to handlebars to show all categories or products.

Assignment 3a Hints - How to store a picture? - Dont convert it into binary and store in DB - Use a folder to include all the photos, mark them by pid. And read it by their pid + ext (e.g 041.jpg) - Never direct access DB from your client - Create a REST API to do the DB work - http://www.nodewiz.biz/nodejs-rest-api-with-mysqland-express/ - Restify - Remember to do the authentication first