Network Programming Lecture 4: Message Posting

Slides:



Advertisements
Similar presentations
© 2014 IBM Corporation Empowering the IBM ecosystem Introduction to Developing applications using node.js on Bluemix IBM Ecosystem Development Instructors.
Advertisements

1 Configuring Internet- related services (April 22, 2015) © Abdou Illia, Spring 2015.
XP Browser and Basics1. XP Browser and Basics2 Learn about Web browser software and Web pages The Web is a collection of files that reside.
ASP Tutorial. What is ASP? ASP (Active Server Pages) is a Microsoft technology that enables you to make dynamic and interactive web pages. –ASP usually.
Browser and Basics Tutorial 1. Learn about Web browser software and Web pages The Web is a collection of files that reside on computers, called.
1 of 5 This document is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS DOCUMENT. © 2007 Microsoft Corporation.
RESTful Web Development With Nodejs and Express. REST Stands for REpresentational State Transfer Has the following constraints: ◦Client-Server ◦Stateless.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
1 ADVANCED MICROSOFT WORD Lesson 15 – Creating Forms and Working with Web Documents Microsoft Office 2003: Advanced.
Chapter 9 Collecting Data with Forms. A form on a web page consists of form objects such as text boxes or radio buttons into which users type information.
Presented by…. Group 2 1. Programming language 2Introduction.
MEAN Stack c0nrad. Overview Day 1: – MEAN Stack – NodeJS Mini Cat Fact Spammer – MongoDB Cat Profiles – Express Catbook API (Facebook for cats) Day 2:
Tutorial 1: Getting Started with Adobe Dreamweaver CS4.
LiveCycle Data Services Introduction Part 2. Part 2? This is the second in our series on LiveCycle Data Services. If you missed our first presentation,
Chapter 8 Collecting Data with Forms. Chapter 8 Lessons Introduction 1.Plan and create a form 2.Edit and format a form 3.Work with form objects 4.Test.
 Whether using paper forms or forms on the web, forms are used for gathering information. User enter information into designated areas, or fields. Forms.
3 Copyright © 2004, Oracle. All rights reserved. Working in the Forms Developer Environment.
ASP. What is ASP? ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information.
1 WWW. 2 World Wide Web Major application protocol used on the Internet Simple interface Two concepts –Point –Click.
IS-907 Java EE World Wide Web - Overview. World Wide Web - History Tim Berners-Lee, CERN, 1990 Enable researchers to share information: Remote Access.
Module: Software Engineering of Web Applications Chapter 2: Technologies 1.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
ASP-2-1 SERVER AND CLIENT SIDE SCRITPING Colorado Technical University IT420 Tim Peterson.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
Chapter 5 Using a Template to Create a Resume and Sharing a Finished Document Microsoft Word 2013.
Chapter 1 Getting Started with ASP.NET Objectives Why ASP? To get familiar with our IDE (Integrated Development Environment ), Visual Studio. Understand.
ClickOnce Deployment (One-click Deployment)
Introduction to MEAN (JavaScript Full Stack Development)
NodeJS and MEAN cs6320.
Data Virtualization Tutorial… SSL with CIS Web Data Sources
NodeJS.
Working in the Forms Developer Environment
WWU Hackathon May 6 & 7.
Node.js Express Web Applications
Data Virtualization Tutorial… CORS and CIS
Node.js Express Web Services
NodeJS and MEAN Prof. L. Grewe.
Tutorial 6 Topic: jQuery and jQuery Mobile Li Xu
Basic XHTML Tables XHTML tables—a frequently used feature that organizes data into rows and columns. Tables are defined with the table element. Table.
CMPE 280 Web UI Design and Development October 19 Class Meeting
3 Things Everyone Knows About Node JS That You Don't
Web UI Basics ITM 352.
Getting web pages First we need to get the webpage by issuing a HTTP request. The best option for this is the requests library that comes with Anaconda:
Build Better Apps with MEAN.
WEB API.
NodeJS coding basics L. Grewe.
03 | Building a Backend with Socket.IO and Mongo
Configuring Internet-related services
Managing a Web Server and Files
HTTP GET vs POST SE-2840 Dr. Mark L. Hornick.
Web Programming Language
AMP Cache Sandeep Davu.
SEEM4570 Tutorial 5: jQuery + jQuery Mobile
Lecture 5: Functions and Parameters
Lecture 17: Web Service and post
Lecture 16: Writing Your Own Web Service
RESTful Web Services.
Web API with Angular 2 Front End

WEB SERVICES From Chapter 19, Distributed Systems
Lecture 12: The Fetch Api and AJAx
CSc 337 Lecture 1: post.
Lecture 14: JSON and Web SERVICES
Chengyu Sun California State University, Los Angeles
Building Apps in Azure with only Scripting Skills
ClickOnce Deployment (One-click Deployment)
CSc 337 Lecture 18: post.
Chengyu Sun California State University, Los Angeles
How to install and manage exchange server 2010 OP Saklani.
Jean-Francois LEBLANC Christian SEBASTIAN
Presentation transcript:

Network Programming Lecture 4: Message Posting Mahmoud El-Gayyar elgayyar@ci.suez.edu.eg Elgayyar.weebly.com

Message Posting Example Create an angular form to get the message from the user Create and Test a service endpoint Send a post from Angular Save the post to MongoDB

Make Angular Post Form 1 We need a form to post a message to our server and we will store it inside MongoDB <div class="container"> <div> <acme-navbar creation-date="main.creationDate"></acme-navbar> </div> <div class="panel panel-default"> <div class="panel-heading">Post A Message</div> <div class="panel-body"> <div class="input-group"> <textarea class="form-control custom-control" style="resize:none"> </textarea> <span class="input-group-addon btn btn-primary" > Post </span> row=3 A <span> element used to color a part of a text: <p>My mother has <span style="color:blue">blue</span> eyes.</p> Definition and Usage The <span> tag is used to group inline-elements in a document. The <span> tag provides no visual change by itself. The <span> tag provides a way to add a hook to a part of a text or a part of a document. http://www.w3schools.com/bootstrap/bootstrap_buttons.asp Main.html

Make Angular Post Form 2 What happens when we press the post button ?! Angular Controller: allow us to bind things to the view or layout such as variables and functions Separates logic from layout and interface We need to bind a post function to the post button through our controller Clean the controller from functions that we are not going to use and add the post message ngInject It's needed for the application to work when minified. If you remove it and use optimize version (serve:dist) it should fail. export class MainController { constructor () { 'ngInject'; } postMessage() {console.log("post"); } } Main.controller.js

Make Angular Post Form 3 Now we would like to bind our button to the function in the controller Open index.rout.js to know the Class and the reference of the Angular Controller Test your app in the browser Commit your work to your repo. <div class="input-group"> <textarea class="form-control custom-control" style="resize:none"> </textarea> <span class="input-group-addon btn btn-primary" ng-click=“main.postMessage()”> Post </span> </div> Angular Directive Main.html

Message Posting Example Create an angular form to get the message from the user Create and Test a service endpoint Send a post from Angular Save the post to MongoDB

Intialize a Package File No Generator for backend, so we do it manually, create a package JSON file Give a name: my-message-backend Version: 1.0.0 Leave rest as defaults Install Express Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. When you deploy on a new host, all dependencies in the package file will be installed automatically ../dev/back-end> npm init Save in dependency package ../dev/back-end> npm install express -save Angular Directive

Create a Server Create server.js file Include express library, getting a reference in a variable called express Initiate the express app in an app variable Start a server and listen on port 5000 Test your server var express=require ('express'); var app=express(); var server=app.listen(5000, function(){ console.log(“listening on:”, server.address().port); }); Angular Directive ../dev/back-end> node server.js Server.js

Create an Endpoint Create an Endpoint that you can access from a URL in the browser Create a post endpoint on address /api/message and call back function with request and response app.post(“/api/message”, function (req,res){ console.log(req.body); res.status(200); //OK response }); Angular Directive Server.js

Test The Endpoint Get Postman Chrome application and add to chrome Close and rerun your server and click on send command You get undefined as you are not supporting JSON format To understand JSON data, we need to install body parser node package Add and use in your app Angular Directive ../dev/back-end> npm install body-parser -save var bodyParser=require (‘body-parser'); app.use(bodyParser.json()); Server.js

Message Posting Example Create an angular form to get the message from the user Create and Test a service endpoint Send a post from Angular Save the post to MongoDB

Send Post through $http Use the $http angular service to send the message But, the service can’t be used without injecting it in the constructor Test Your App export class MainController { constructor () { 'ngInject'; } postMessage() { $http.post(‘http://localhost:5000/api/message’, {msg: ‘hello’}); } export class MainController { constructor ($http) { 'ngInject'; this.$http=$http; } postMessage() { this.$http.post(‘http://localhost:5000/api/message’, {msg: ‘hello’}); } ngInject It's needed for the application to work when minified. If you remove it and use optimize version (serve:dist) it should fail. Main.controller.js

Resolve the Cross-Origin Error The App that runs on port 3000 has no access permission to access the server that runs on Port 5000 To solve we have to enable Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the resource originated Add custom middleware in server to allow from any origin and specific headers var bodyParser=require (‘body-parser'); app.use(bodyParser.json()); app.use (function(req,res,next){ res.header(“Access-Control-Allow-Origin”, “*”); //any location res.header(“Access-Control-Allow-Headers”, “Content-Type, Authorization”); next(); }); Add custom middleware() Next() to go to next middleware and not freeze as it is a custom one $ curl -v -X GET -H "Content-Type: application/json" -H "Authorization: Server.js

Get the Message from TextArea Add a model directive Use this in your controller <textarea … ng-model= “main.message”> </textarea> Main.html postMessage() { this.$http.post(‘http://localhost:5000/api/message’, {msg: this.message}); } ngInject It's needed for the application to work when minified. If you remove it and use optimize version (serve:dist) it should fail. Main.controller.js

Message Posting Example Create an angular form to get the message from the user Create and Test a service endpoint Send a post from Angular Save the post to MongoDB

Connect to Mongo DB You have two options to deal with mongodb Official mongodb driver (we are going to use in this example) Mongoose Install mongodb node driver no -- save as we are going to use mongoose later for deployment Include in your server and connect to your database (test db) ../dev/back-end> npm install mongodb var bodyParser=require (‘body-parser'); var mongo= require (‘mongodb’).MongoClient; app.use(bodyParser.json()); … mongo.connect(“mongodb://localhost:27017/test”,function (err,db){ if(!err){ console.log(“we are connected to mongo”); } }); Add custom middleware() Next() to go to next middleware and not freeze as it is a custom one $ curl -v -X GET -H "Content-Type: application/json" -H "Authorization: Server.js

Insert message in a Collection Create new collection called messages and insert a message Verify that the message has been inserted Open the mongo console Display messages collection mongo.connect(“mongodb://localhost:27017/test”,function (err,db){ if(!err){ console.log(“we are connected to mongo”); db.collection(‘messages’).insertOne({‘msg’:’test’}); } }); Add custom middleware() Next() to go to next middleware and not freeze as it is a custom one $ curl -v -X GET -H "Content-Type: application/json" -H "Authorization: > db.messages.find() Server.js

Insert the coming in a Collection Add a global variable to hold the db object. var database; mongo.connect(“mongodb://localhost:27017/test”,function (err,db){ if(!err){ console.log(“we are connected to mongo”); db.collection(‘messages’).insertOne({‘msg’:’test’}); database=db; } }); app.post('/api/message', function (req,res){ console.log(req.body); database.collection(‘messages’).insertOne(req.body); res.status(200); //OK response Add custom middleware() Next() to go to next middleware and not freeze as it is a custom one $ curl -v -X GET -H "Content-Type: application/json" -H "Authorization: Server.js