CS193X: Web Programming Fundamentals: Spring 2017 Victoria Kirst (Vrk@stanford - Edu)
CS193X: Web Programming Fundamentals: Spring 2017 Victoria Kirst (Vrk@stanford - Edu)
Web Programming
Fundamentals
Spring 2017
Victoria Kirst
([email protected])
Schedule
Today:
- npm
- Express
- fetch() to localhost
- package.json
- HW4 due tonight, late cutoff Friday
- HW5 released sometime tomorrow
Friday:
- Saving and retrieving data
Node installation; lecture code
NodeJS:
- A JavaScript runtime written in C++.
- Can interpret and execute JavaScript.
- Includes support for the NodeJS API.
NodeJS API:
- A set of JavaScript libraries that are useful for creating
server programs.
V8 (from Chrome):
- The JavaScript interpreter ("engine") that NodeJS uses
to interpret, compile, and execute JavaScript code
node command
$ node
> let x = 5;
undefined
> x++
5
> x
6
node command
server.js (GitHub):
Node for servers
Client Server
(Routing, etc…)
But when we run our server locally, isn't there only one computer
involved?
Local server?
Client Server
(Routing, etc…)
But when we run our server locally, isn't there only one computer
involved? A: Yes, when we execute our Node server and access it via
localhost, our laptop is both the client and the server machine.
Running a server
Server
Server
Running a server
Server
(Routing, etc…)
"OK, my response is
'Hello World'" vrktest.localtunnel.me
Client
(your laptop)
localtunnel demo
Notice:
- If I kill my "node server.js" process, the URL no longer
works (will timeout)
(In other words: Unless you are a) expecting >1000 simultaneous requests to
your web server, and b) picky about exactly how those requests are served, you
really don't need to be deploying your server via AWS, Google Cloud Platform,
or other IaaS. It's not that AWS/GCP isn't worth learning; it's just not the first
thing you need to learn.)
localtunnel on local machine
(Routing, etc…)
(Routing, etc…)
"Hey here's
this GET
request"
(Routing, etc…)
"GET
vrktest.localtunnel.
me"
vrktest.localtunnel.me
Server
(Victoria's laptop)
Client
(also Victoria's laptop)
localtunnel response
"The response is 'Hello
World'"
(Routing, etc…)
"OK, my
response is
'Hello World'"
(Routing, etc…)
"The response is
'Hello World'"
vrktest.localtunnel.me
Server
(Victoria's laptop)
Client
(also Victoria's laptop)
localtunnel is not needed
(Routing, etc…)
(Routing, etc…)
vrktest.localtunnel.me
But since the client and the server are on the same
machine… this routing/proxying step is unnecessary.
localhost
localhost:3000
Browser: "Hey
Operating System, OS: "Hey Node,
send a GET request you're the thing
to localhost:3000" running on my own
port 3000. Here's this
localhost:3000 GET request."
Localhost response
app.method(path, handler)
- Specifies how the server should handle HTTP method
requests made to URL/path
- The function callback will fire every time there's a new
response.
- This example is saying: When there's a GET request to
http://localhost:3000/, respond with the text "Hello World!"
More routes
e.g.
This line of code makes our server now start serving the
files in the 'public' directory directly.
Server static data
GitHub
Sending data to the server
Route parameters
GitHub
Route parameters
GitHub
Query parameters
GitHub
Query params with POST
GitHub
GitHub
POST message body
GitHub
1. Route parameters
2. GET request with query parameters
(DISCOURAGED: POST with query parameters)
3. POST request with message body
https://api.spotify.com/v1/search?type=album&q=the%20wee
knd&limit=10
- q is required but might have spaces, so it is a query
parameter
- limit is optional and is a query parameter
- type is required but is a query parameter (breaks
convention)
Notice both searches are GET requests, too
package.json
Installing dependencies
GitHub
Saving deps to package.json
- This also allows people who have downloaded your code from
GitHub to install all your dependencies with one command instead
of having to install all dependencies individually.
npm scripts