Lecture 16: Writing Your Own Web Service

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

Internet and World Wide Web Technologies E-commerce Book Chapter 3 pages Concentrate on
CPSC 441: FTP & SMTP1 Application Layer: FTP & Instructor: Carey Williamson Office: ICT Class.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
Chapter Eleven An Introduction to TCP/IP. Objectives To compare TCP/IP’s layered structure to OSI To review the structure of an IP address To look at.
Syllabus outcomes Describes and applies problem-solving processes when creating solutions Designs, produces and evaluates appropriate solutions.
Networking Basics TCP/IP TRANSPORT and APPLICATION LAYER Version 3.0 Cisco Regional Networking Academy.
 TCP/IP is the communication protocol for the Internet  TCP/IP defines how electronic devices should be connected to the Internet, and how data should.
Network Protocols. Why Protocols?  Rules and procedures to govern communication Some for transferring data Some for transferring data Some for route.
FTP (File Transfer Protocol) & Telnet
© 2007 Cisco Systems, Inc. All rights reserved.Cisco Public ITE PC v4.0 Chapter 1 1 Network Services Networking for Home and Small Businesses – Chapter.
Chapter 1: Introduction to Web Applications. This chapter gives an overview of the Internet, and where the World Wide Web fits in. It then outlines the.
HTTP HTTP stands for Hypertext Transfer Protocol. It is an TCP/IP based communication protocol which is used to deliver virtually all files and other.
CCNA1 v3 Module 11 v3 CCNA 1 Module 11 JEOPARDY S Dow.
1 CSC111H Client-Server: An Introduction Dennis Burford
TCP/IP fundamentals Unit objectives Discuss the evolution of TCP/IP Discuss TCP/IP fundamentals.
File Transfer Protocol (FTP)
Data Streams David Meredith Source Chapter 19 of – Shiffman, D. (2008). Learning Processing. Morgan Kaufmann, Burlington, MA. ISBN:
1 Introductory material. This module illustrates the interactions of the protocols of the TCP/IP protocol suite with the help of an example. The example.
The Inter-network is a big network of networks.. The five-layer networking model for the internet.
Internet Protocol B Bhupendra Ratha, Lecturer School of Library and Information Science Devi Ahilya University, Indore
Application Layer Khondaker Abdullah-Al-Mamun Lecturer, CSE Instructor, CNAP AUST.
TCP/IP (Transmission Control Protocol / Internet Protocol)
Network Programming All networked computers have an IP Address – Unique – In the form of xxx.xxx.xxx.xxx ( ) – 32 bits = ~4 billion possibilities.
The Module Road Map Assignment 1 Road Map We will look at… Internet / World Wide Web Aspects of their operation The role of clients and servers ASPX.
Protocols COM211 Communications and Networks CDA College Olga Pelekanou
Protocols Monil Adhikari. Agenda Introduction Port Numbers Non Secure Protocols FTP HTTP Telnet POP3, SMTP Secure Protocols HTTPS.
Slides based on Carey Williamson’s: FTP & SMTP1 File Transfer Protocol (FTP) r FTP client contacts FTP server at port 21, specifying TCP as transport protocol.
Application of the Internet 1998/12/09 KEIO University, JAPAN Mikiyo
Computer Network Architecture Lecture 6: OSI Model Layers Examples 1 20/12/2012.
Networking Mehdi Einali Advanced Programming in Java 1.
IST 201 Chapter 11 Lecture 2. Ports Used by TCP & UDP Keep track of different types of transmissions crossing the network simultaneously. Combination.
Enumeration.
Tiny http client and server
Data communication and Networks
Instructor Materials Chapter 5 Providing Network Services
Development of Web Applications - Introduction
Web Development Web Servers.
v3 JEOPARDY CCNA 1 Module 11 CCNA1 v3 Module 11 Galo Valencia
Data Virtualization Tutorial… CORS and CIS
Computing with C# and the .NET Framework
Network Wiring and Reference
Unit 4: Transport protocols
Some bits on how it works
Understand the OSI Model Part 2
Networking for Home and Small Businesses – Chapter 6
14-мавзу. Cookie, сеанс, FTP и технологиялари
Networking for Home and Small Businesses – Chapter 6
Internet Protocol Mr. Paulk.
Topic 5: Communication and the Internet
Advanced Programming in Java
TCP/IP Networking An Example
Application layer Lecture 7.
Abel Sanchez, John R. Williams
Introduction to Client/Server Computing
Lecture 17: Web Service and post
Lecture 12: The Fetch Api and AJAx
TCP/IP Protocol Suite: Part 2, Application Layer
CSc 337 Lecture 1: post.
Networking for Home and Small Businesses – Chapter 6
Lecture 14: JSON and Web SERVICES
Chapter 7 Network Applications
Chengyu Sun California State University, Los Angeles
Chapter 2 Application Layer
Computer Networks Protocols
CSc 337 Lecture 18: post.
Chengyu Sun California State University, Los Angeles
Lecture 15: Writing Your Own Web Service
Networking Theory (part 2)
Objectives: 1.Identify different internet protocol (IP) 2.Configure sample of IP address 3.Appreciate and relate protocol in our life.
Presentation transcript:

Lecture 16: Writing Your Own Web Service CSc 337 Lecture 16: Writing Your Own Web Service

Basic web service // CSC 337 hello world server const express = require("express"); const app = express(); app.use(express.static('public')); app.get('/', function (req, res) { res.header("Access-Control-Allow-Origin", "*"); res.send('Hello World!'); }) app.listen(3000);

require() const express = require("express"); The NodeJS require() statement loads a module, similar to import in Java or Python. We can require() modules included with NodeJS, or modules we've written ourselves.

listen() app.listen(3000); The listen() function will start accepting connections on the given port number.

Ports and binding port: In the context of networking, a "logical" (as opposed to a physical) connection place A number from 0 to 65535 (16-bit unsigned integer) Used to distinguish a message for one program from another When you start running a server process, you tell the operating system what port number to associate with it. This is called binding.

Port defaults There are many well-known ports, i.e. the ports that will be used by default for particular protocols: 21: File Transfer Protocol (FTP) 22: Secure Shell (SSH) 23: Telnet remote login service 25: Simple Mail Transfer Protocol (SMTP) 53: Domain Name System (DNS) service 80: Hypertext Transfer Protocol (HTTP) used in the World Wide Web 110: Post Office Protocol (POP3) 119: Network News Transfer Protocol (NNTP) 123: Network Time Protocol (NTP) 143: Internet Message Access Protocol (IMAP) 161: Simple Network Management Protocol (SNMP) 194: Internet Relay Chat (IRC) 443: HTTP Secure (HTTPS)

Development Server - We have been using 3000 in examples but you can use whatever number you want app.listen(3000);

Avoiding CORS Errors Allows us to access our code on localhost. otherwise NodeJS thinks we are on different machines app.use(express.static('public'));

Making a REquest The type of request we are making right now is GET req: an object representing the request res: an object representing the response app.get('/', function (req, res) { res.header("Access-Control-Allow-Origin", "*"); res.send('Hello World!'); })

Get Query Parameters in Express Query parameters are saved in req.query app.get('/', function (req, res) { res.header("Access-Control-Allow-Origin", "*"); const queryParams = req.query; console.log(queryParams); const name = req.query.name; res.send('Hello' + name); })

Exercise Write a web service that takes an exponent and base as parameters and outputs the based raised to the exponent

Generating JSON Create a JSON object: var data = {}; Add any data you want in your JSON to this: data["name"] = "Merlin"; Once you have put together the data you want: var to_send = JSON.stringify(data);

Exercise Build a web service that takes two numbers as parameters and outputs JSON. For example, if the service were passed 2 for num1 and 3 for num2: { "plus" : 5, "minus": 1, "times": 6, "divide": 1.5 }

Reading from a file let file = fs.readFileSync(file_name, 'utf8'); You can read from a file with the above code. Returns the contents of the file as a string.

Exercise Read data from a file called books.txt Make sure books.txt is in the same folder as your web service Output JSON that that contains a list of all books in the file. Each list item should contain the book's name, author, category, year and price as individual items.

Exercise - part 2 Add a parameter to your service so that when the user supplies the parameter books with a value of a category name your service will only output books in that category. If the user doesn't supply a parameter your service should produce all books. http://localhost:3000?books=computer

Exercise - part 3 If there are no books that are in the category that the user supplies have your service return a 410 status and a message about the category not being found. Set the status with the following code: res.status(410);