CSc 337 LECTURE 16: WRITING YOUR OWN WEB SERVICE Basic web service - - PowerPoint PPT Presentation

csc 337
SMART_READER_LITE
LIVE PREVIEW

CSc 337 LECTURE 16: WRITING YOUR OWN WEB SERVICE Basic web service - - PowerPoint PPT Presentation

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) {


slide-1
SLIDE 1

CSc 337

LECTURE 16: WRITING YOUR OWN WEB SERVICE

slide-2
SLIDE 2

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);

slide-3
SLIDE 3

require()

const express = require("express"); The NodeJS require() statement loads a module, similar to import in Java

  • r Python.

We can require() modules included with NodeJS, or modules we've written ourselves.

slide-4
SLIDE 4

listen()

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

slide-5
SLIDE 5

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.

slide-6
SLIDE 6

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)

slide-7
SLIDE 7

Development Server

  • We have been using 3000 in examples but you can use whatever

number you want app.listen(3000);

slide-8
SLIDE 8

Avoiding CORS Errors

Allows us to access our code on localhost.

  • otherwise NodeJS thinks we are on different machines

app.use(express.static('public'));

slide-9
SLIDE 9

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!'); })

slide-10
SLIDE 10

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); })

slide-11
SLIDE 11

Exercise

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

slide-12
SLIDE 12

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);
slide-13
SLIDE 13

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 }

slide-14
SLIDE 14

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.

slide-15
SLIDE 15

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.

slide-16
SLIDE 16

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

  • utput books in that category. If the user doesn't supply a parameter your

service should produce all books. http://localhost:3000?books=computer

slide-17
SLIDE 17

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);