Lecture 15: Writing Your Own Web Service

Slides:



Advertisements
Similar presentations
OpenSocial CS : Survey of Contemporary Technologies.
Advertisements

© 2014 IBM Corporation Empowering the IBM ecosystem Introduction to Developing applications using node.js on Bluemix IBM Ecosystem Development Instructors.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
RESTful Web Development With Nodejs and Express. REST Stands for REpresentational State Transfer Has the following constraints: ◦Client-Server ◦Stateless.
PHP : Hypertext Preprocessor
Subversion (SVN) Tutorial Source:
The Inter-network is a big network of networks.. The five-layer networking model for the internet.
Introduction to Programming Workshop 6 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
PROPERTIES OF EXPONENTS
INFO 344 Web Tools And Development CK Wang University of Washington Spring 2014.
CompSci 101 Introduction to Computer Science March 31, 2015 Prof. Rodger Thanks to Elizabeth Dowd for giving this lecture Review for exam.
Drill-Through Features Cognos 8 BI. Objectives  In this module we will examine:  Cognos 8 Drill Through Overview  Model / Package Drill Through  Cross.
COSC 2328 – Web Programming.  PHP is a server scripting language  It’s widely-used and free  It’s an alternative to Microsoft’s ASP and Ruby  PHP.
Node.JS introduction. What is Node.JS? v8 JavaScript runtime Event driven Non-blocking standard libraries Most APIs speak streams Provides a package manager.
0 Copyright 2012 FUJITSU Interstage BOP SQL Query Tutorial Todd Palmer October 2012.
CSE 154 LECTURE 18: FORMS AND UPLOADING FILES. Exercise: Baby name web service JSON Modify our babynames.php service to produce its output as JSON. For.
BIT 286: Web Applications ASP.Net MVC. Objectives Applied MVC overview Controllers Intro to Routing Views ‘Convention over configuration’ Layout files.
1 Using MVC 6. MVC vs. ASP Web Forms Both run under ASP.NET Can coexist In Web Forms, browser requests page. xxx.aspx and xxx.aspx.cs on the server Page.
Lecture 16: File I/O; Functions
NodeJS and MEAN cs6320.
GC16/3011 Functional Programming Lecture 15 Lists, Trees and Graphs
Recap: If, elif, else If <True condition>:
Business Directory REST API
Chapter 6: User-Defined Functions I
Web-Technology Lecture 11.
Node.js Express Web Applications
BIT116: Scripting Loops.
Designing For Testability
Intro to Python Programming – Part II
Express with Node L. Grewe.
NodeJS and MEAN Prof. L. Grewe.
Logging In ASP.Net Core 1.1.
CSE 154 Lecture 24: JSON.
Build Better Apps with MEAN.
Network Programming Lecture 4: Message Posting
Week 04 (Final Project) Node.js Week 04 (Final Project)
Express with Node L. Grewe. Feature 1 Project structure.
Testing REST IPA using POSTMAN
Introduction to Programming
NodeJS coding basics L. Grewe.
Logging In ASP.Net Core 2.0.
welcome to: Latinx Tech PDX
More Loops.
More Loops.
Working with Server-side Scripts
1. Open Visual Studio 2008.
INFO/CSE 100, Spring 2005 Fluency in Information Technology
Abel Sanchez, John R. Williams
Programming in JavaScript
Lesson 12.
Video list editor BIS1523 – Lecture 24.
Web Programming Language
Python programming exercise
Objective Use multiplication properties of exponents to evaluate and simplify expressions.
CS150 Introduction to Computer Science 1
Programming in JavaScript
Programming in JavaScript
Programming in JavaScript
BIT116: Scripting Functions.
Lecture 17: Web Service and post
Lecture 16: Writing Your Own Web Service
RESTful Web Services.
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
CSc 337 Lecture 18: post.
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Lecture 15: Writing Your Own Web Service CSc 337 Lecture 15: 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);

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. To use you will need to install the fs module npm install fs

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

Reading all files in a directory let files = fs.readDirSync(directory_name); You can read the names of all of the files in a directory with the above code. Returns the names of the files in a directory as a list of strings. Pass in "." to get all of the files in the current directory. To use you will need to install the fs module npm install fs