Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and...

717
Video 4.1 Intro to Node.js Environment Setup Chris Murphy SD4x-4.1 1

Transcript of Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and...

Page 1: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Video 4.1

Intro to Node.js Environment Setup

Chris Murphy

SD4x-4.1 1

Page 2: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Review: How does a Web Browser Work?

• The World Wide Web utilizes Hypertext Transfer Protocol (HTTP) to transfer documents

Client Server

Client sends request

Server sends response

1

2

2SD4x-4.1

Page 3: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Review: How does a Web Browser Work?

• The World Wide Web utilizes Hypertext Transfer Protocol (HTTP) to transfer documents

Client Server

Client sends request

Server sends response

1

2

3SD4x-4.1

Page 4: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Review: How does a Web Browser Work?

• The World Wide Web utilizes Hypertext Transfer Protocol (HTTP) to transfer documents

Client Server

Client sends request

Server sends response

1

2

4SD4x-4.1

Page 5: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

What does the Web Server do?

• Listen for and accept incoming HTTP requests

• Parse the HTTP request to determine what is being requested

• Locate (and/or create) the resource being requested

• Construct and send back the HTTP response

SD4x-4.1 5

Page 6: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Node.js

• Asynchronous, event-driven JavaScript runtime environment for building web applications

• Treats HTTP requests as events that invoke callback functions/handlers that construct the HTTP response

• Also includes a package manager to simplify the deployment of JavaScript apps

SD4x-4.1 6

Page 7: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Installing Node.js

• You can install Node.js by downloading, running, and finishing the package installer available here:

• https://nodejs.org/en/download/

• Check that installation is correct using: node –v

• Update modules using: npm install npm –g

SD4x-4.1 7

Page 8: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Setting up a new project

• Create a new folder for your project

• Use Terminal, Command Prompt, etc. to navigate to that folder

• Set up a new project by running: npm init

• You will be prompted to enter some information about your project

• Specify “index.js” as your entry point

SD4x-4.1 8

Page 9: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Setting up a new project

• Your project folder should now have a package.json configuration file

{

"name": "helloworld",

"version": "1.0.0",

"description": "A basic hello world app",

"main": "index.js",

"scripts": {

"test": "echo \"Error: no test specified\" && exit 1"

},

"author": "edX Learner",

"license": "ISC"

}

SD4x-4.1 9

Page 10: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express

• Express is a web application framework that sits on top of a Node.js server

• Express helps you modularize and streamline your web application

• Within Express, you can organize your app in many ways:

• Define separate modules that have different responsibilities

• Handle requests via different routes and routers

• Split each step in the processing of a request into Middlewares

SD4x-4.1 10

Page 11: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Adding Express

• To use Express, run the following from the folder where you created your Node.js app: npm install express --save

• The Express package will be downloaded to the project and added to your package.json file as a dependency

• Package: a package is a module of JavaScript code, usually with a specific purpose, that can be re-used and assembled with other modules

• Dependency: A dependency is a piece of code that your program relies on to work correctly

SD4x-4.1 11

Page 12: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Configuration

• Your package.json file will now have a new section called dependencies

• npm can refer to this in the future and re-download or update your packages as needed

SD4x-4.1 12

{

"name": "helloworld",

"version": "1.0.0",

"description": "A basic hello world app",

"main": "index.js",

"scripts": {

"test": "echo \"Error: no test specified\" && exit 1"

},

"author": "edX Learner",

"license": "ISC",

"dependencies": {

"express": "^4.15.3"

}

}

Page 13: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Configuration

• Your package.json file will now have a new section called dependencies

• npm can refer to this in the future and re-download or update your packages as needed

SD4x-4.1 13

{

"name": "helloworld",

"version": "1.0.0",

"description": "A basic hello world app",

"main": "index.js",

"scripts": {

"test": "echo \"Error: no test specified\" && exit 1"

},

"author": "edX Learner",

"license": "ISC",

"dependencies": {

"express": "^4.15.3"

}

}

Page 14: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Configuration

• Your package.json file will now have a new section called dependencies

• npm can refer to this in the future and re-download or update your packages as needed

SD4x-4.1 14

{

"name": "helloworld",

"version": "1.0.0",

"description": "A basic hello world app",

"main": "index.js",

"scripts": {

"test": "echo \"Error: no test specified\" && exit 1"

},

"author": "edX Learner",

"license": "ISC",

"dependencies": {

"express": "^4.15.3"

}

}

Page 15: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Configuration

• Your package.json file will now have a new section called dependencies

• npm can refer to this in the future and re-download or update your packages as needed

SD4x-4.1 15

{

"name": "helloworld",

"version": "1.0.0",

"description": "A basic hello world app",

"main": "index.js",

"scripts": {

"test": "echo \"Error: no test specified\" && exit 1"

},

"author": "edX Learner",

"license": "ISC",

"dependencies": {

"express": "^4.15.3"

}

}

Page 16: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Configuration

• Your package.json file will now have a new section called dependencies

• npm can refer to this in the future and re-download or update your packages as needed

SD4x-4.1 16

{

"name": "helloworld",

"version": "1.0.0",

"description": "A basic hello world app",

"main": "index.js",

"scripts": {

"test": "echo \"Error: no test specified\" && exit 1"

},

"author": "edX Learner",

"license": "ISC",

"dependencies": {

"express": "^4.15.3"

}

}

Page 17: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Hello World

• Create an file named index.js in your Node.jsproject root directory with the following contents:

var express = require('express');

var app = express();

app.use('/', (req, res) => {

res.send('Hello World!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.1 17

Page 18: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Hello World

• Create an file named index.js in your Node.jsproject root directory with the following contents:

var express = require('express');

var app = express();

app.use('/', (req, res) => {

res.send('Hello World!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.1 18

Page 19: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Hello World

• Create an file named index.js in your Node.jsproject root directory with the following contents:

var express = require('express');

var app = express();

app.use('/', (req, res) => {

res.send('Hello World!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.1 19

Page 20: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Hello World

• Create an file named index.js in your Node.jsproject root directory with the following contents:

var express = require('express');

var app = express();

app.use('/', (req, res) => {

res.send('Hello World!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.1 20

Page 21: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Hello World

• Create an file named index.js in your Node.jsproject root directory with the following contents:

var express = require('express');

var app = express();

app.use('/', (req, res) => {

res.send('Hello World!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.1 21

Page 22: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Hello World

• Create an file named index.js in your Node.jsproject root directory with the following contents:

var express = require('express');

var app = express();

app.use('/', (req, res) => {

res.send('Hello World!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.1 22

Page 23: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Hello World

• Create an file named index.js in your Node.jsproject root directory with the following contents:

var express = require('express');

var app = express();

app.use('/', (req, res) => {

res.send('Hello World!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.1 23

Page 24: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Hello World

• Create an file named index.js in your Node.jsproject root directory with the following contents:

var express = require('express');

var app = express();

app.use('/', (req, res) => {

res.send('Hello World!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.1 24

Page 25: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Hello World

• Create an file named index.js in your Node.jsproject root directory with the following contents:

var express = require('express');

var app = express();

app.use('/', (req, res) => {

res.send('Hello World!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.1 25

Page 26: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Hello World

• Create an file named index.js in your Node.jsproject root directory with the following contents:

var express = require('express');

var app = express();

app.use('/', (req, res) => {

res.send('Hello World!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.1 26

Page 27: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Hello World

• Create an file named index.js in your Node.jsproject root directory with the following contents:

var express = require('express');

var app = express();

app.use('/', (req, res) => {

res.send('Hello World!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.1 27

Page 28: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Hello World

• Create an file named index.js in your Node.jsproject root directory with the following contents:

var express = require('express');

var app = express();

app.use('/', (req, res) => {

res.send('Hello World!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.1 28

Page 29: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Running Express

• In the project folder, run: node index.js

• When the server starts, you should see “Listening on port 3000” written to the console/screen

• Open a browser on the same computer and go to http://localhost:3000/

SD4x-4.1 29

Page 30: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.1 30

Page 31: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Looking Ahead

• How can the server send different responses for different requests?

• How can the server dynamically generate responses?

• How does the server interact with external data sources?

SD4x-4.1 31

Page 32: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Video 4.2

Node.js Request and Response Objects

Chris Murphy

SD4x-4.2 32

Page 33: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Review

• Web browsers communicate with Web servers via HTTP requests and responses

• Node.js and Express simplify the development of Web servers to handle HTTP requests and create and return HTTP responses

SD4x-4.2 33

Page 34: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Anatomy of an HTTP Request

GET /examples/index.html HTTP/1.1

Host: www.edx.org

User-Agent: Mozilla/4.0

Accept-Language: en-us

Content-Length: 9

a=12&b=34

Request Verb URI HTTP Version

Request Line

Request Headers

Blank Line Separator

Request Message Body

34SD4x-4.2

Page 35: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Node.js/Express Request Objects

• An HTTP Request is represented as an object in the Express app

• The object is passed as a parameter to the callback function/event handler

SD4x-4.2 35

var express = require('express');

var app = express();

app.use('/', (req, res) => {

res.send('Hello World!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 36: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Node.js/Express Request Objects

• An HTTP Request is represented as an object in the Express app

• The object is passed as a parameter to the callback function/event handler

SD4x-4.2 36

var express = require('express');

var app = express();

app.use('/', (req, res) => {

res.send('Hello World!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 37: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Node.js/Express Request Objects

• An HTTP Request is represented as an object in the Express app

• The object is passed as a parameter to the callback function/event handler

SD4x-4.2 37

var express = require('express');

var app = express();

app.use('/', (req, res) => {

res.send('Hello World!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 38: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Node.js/Express Request Objects

• An HTTP Request is represented as an object in the Express app

• The object is passed as a parameter to the callback function/event handler

SD4x-4.2 38

var express = require('express');

var app = express();

app.use('/', (req, res) => {

res.send('Hello World!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 39: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Properties/Functions

SD4x-4.2 39

app.use('/', (req, res) => {

var method = req.method;

var url = req.url;

var agent = req.headers['user-agent'];

agent = req.get('User-Agent');

res.send('Hello World!');

});

Page 40: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Properties/Functions

SD4x-4.2 40

app.use('/', (req, res) => {

var method = req.method;

var url = req.url;

var agent = req.headers['user-agent'];

agent = req.get('User-Agent');

res.send('Hello World!');

});

Page 41: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Properties/Functions

• method: the HTTP Request verb/action

SD4x-4.2 41

app.use('/', (req, res) => {

var method = req.method;

var url = req.url;

var agent = req.headers['user-agent'];

agent = req.get('User-Agent');

res.send('Hello World!');

});

Page 42: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Properties/Functions

• method: the HTTP Request verb/action

• url: the resource that was requested

SD4x-4.2 42

app.use('/', (req, res) => {

var method = req.method;

var url = req.url;

var agent = req.headers['user-agent'];

agent = req.get('User-Agent');

res.send('Hello World!');

});

Page 43: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Properties/Functions

• method: the HTTP Request verb/action

• url: the resource that was requested

• headers: object containing all headers

SD4x-4.2 43

app.use('/', (req, res) => {

var method = req.method;

var url = req.url;

var agent = req.headers['user-agent'];

agent = req.get('User-Agent');

res.send('Hello World!');

});

Page 44: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Properties/Functions

• method: the HTTP Request verb/action

• url: the resource that was requested

• headers: object containing all headers

• get(field): request header field

SD4x-4.2 44

app.use('/', (req, res) => {

var method = req.method;

var url = req.url;

var agent = req.headers['user-agent'];

agent = req.get('User-Agent');

res.send('Hello World!');

});

Page 45: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Anatomy of an HTTP Response

HTTP/1.1 200 OK

Date: Fri, 06 Apr xxxx 09:30:00 GMT

Server: Apache/1.4

Last-Modified: Wed, 04 Apr xxxx

Connection: close

Content-Type: text/html

Content-Length: 228

<!DOCTPYE html><html><head>….

HTTP Version Status Code

Response Line

Response Headers

Blank Line Separator

Response Body (Resource)

45

Page 46: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Node.js/Express Response Objects

• An HTTP Response is also represented as an object in the Express app

• The object is passed as a parameter to the callback function/event handler

SD4x-4.2 46

var express = require('express');

var app = express();

app.use('/', (req, res) => {

res.send('Hello World!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 47: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Node.js/Express Response Objects

• An HTTP Response is also represented as an object in the Express app

• The object is passed as a parameter to the callback function/event handler

SD4x-4.2 47

var express = require('express');

var app = express();

app.use('/', (req, res) => {

res.send('Hello World!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 48: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Response Object Functions

SD4x-4.2 48

app.use('/', (req, res) => {

res.status(200);

res.type('html');

res.write('Hello world!');

res.write('<p>');

res.write('<b>Have a nice day</b>');

res.end();

});

Page 49: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Response Object Functions

SD4x-4.2 49

app.use('/', (req, res) => {

res.status(200);

res.type('html');

res.write('Hello world!');

res.write('<p>');

res.write('<b>Have a nice day</b>');

res.end();

});

Page 50: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Response Object Functions

• status: set the HTTP status code

SD4x-4.2 50

app.use('/', (req, res) => {

res.status(200);

res.type('html');

res.write('Hello world!');

res.write('<p>');

res.write('<b>Have a nice day</b>');

res.end();

});

Page 51: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Response Object Functions

• status: set the HTTP status code

• type: set the HTTP content type

SD4x-4.2 51

app.use('/', (req, res) => {

res.status(200);

res.type('html');

res.write('Hello world!');

res.write('<p>');

res.write('<b>Have a nice day</b>');

res.end();

});

Page 52: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Response Object Functions

• status: set the HTTP status code

• type: set the HTTP content type

• write: add content to the body of the response

SD4x-4.2 52

app.use('/', (req, res) => {

res.status(200);

res.type('html');

res.write('Hello world!');

res.write('<p>');

res.write('<b>Have a nice day</b>');

res.end();

});

Page 53: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Response Object Functions

• status: set the HTTP status code

• type: set the HTTP content type

• write: add content to the body of the response

SD4x-4.2 53

app.use('/', (req, res) => {

res.status(200);

res.type('html');

res.write('Hello world!');

res.write('<p>');

res.write('<b>Have a nice day</b>');

res.end();

});

Page 54: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Response Object Functions

• status: set the HTTP status code

• type: set the HTTP content type

• write: add content to the body of the response

SD4x-4.2 54

app.use('/', (req, res) => {

res.status(200);

res.type('html');

res.write('Hello world!');

res.write('<p>');

res.write('<b>Have a nice day</b>');

res.end();

});

Page 55: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Response Object Functions

• status: set the HTTP status code

• type: set the HTTP content type

• write: add content to the body of the response

• end: send the response and close the connection

SD4x-4.2 55

app.use('/', (req, res) => {

res.status(200);

res.type('html');

res.write('Hello world!');

res.write('<p>');

res.write('<b>Have a nice day</b>');

res.end();

});

Page 56: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Combining Requests and Responses

SD4x-4.2 56

app.use('/', (req, res) => {

var name = req.query.name; // e.g. /?name=devesh

res.status(200).type('html');

if (name) {

res.write('Hi, ' + name + "it's nice to see you.");

}

else {

res.write('Welcome, guest!');

}

res.end();

});

Page 57: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Combining Requests and Responses

SD4x-4.2 57

app.use('/', (req, res) => {

var name = req.query.name; // e.g. /?name=devesh

res.status(200).type('html');

if (name) {

res.write('Hi, ' + name + "it's nice to see you.");

}

else {

res.write('Welcome, guest!');

}

res.end();

});

Page 58: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Combining Requests and Responses

SD4x-4.2 58

app.use('/', (req, res) => {

var name = req.query.name; // e.g. /?name=devesh

res.status(200).type('html');

if (name) {

res.write('Hi, ' + name + "it's nice to see you.");

}

else {

res.write('Welcome, guest!');

}

res.end();

});

Page 59: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Combining Requests and Responses

SD4x-4.2 59

app.use('/', (req, res) => {

var name = req.query.name; // e.g. /?name=devesh

res.status(200).type('html');

if (name) {

res.write('Hi, ' + name + "it's nice to see you.");

}

else {

res.write('Welcome, guest!');

}

res.end();

});

Page 60: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Combining Requests and Responses

SD4x-4.2 60

app.use('/', (req, res) => {

var name = req.query.name; // e.g. /?name=devesh

res.status(200).type('html');

if (name) {

res.write('Hi, ' + name + "it's nice to see you.");

}

else {

res.write('Welcome, guest!');

}

res.end();

});

Page 61: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Combining Requests and Responses

SD4x-4.2 61

app.use('/', (req, res) => {

var name = req.query.name; // e.g. /?name=devesh

res.status(200).type('html');

if (name) {

res.write('Hi, ' + name + "it's nice to see you.");

}

else {

res.write('Welcome, guest!');

}

res.end();

});

Page 62: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Combining Requests and Responses

SD4x-4.2 62

app.use('/', (req, res) => {

var name = req.query.name; // e.g. /?name=devesh

res.status(200).type('html');

if (name) {

res.write('Hi, ' + name + "it's nice to see you.");

}

else {

res.write('Welcome, guest!');

}

res.end();

});

Page 63: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Combining Requests and Responses

SD4x-4.2 63

app.use('/', (req, res) => {

var name = req.query.name; // e.g. /?name=devesh

res.status(200).type('html');

if (name) {

res.write('Hi, ' + name + "it's nice to see you.");

}

else {

res.write('Welcome, guest!');

}

res.end();

});

Page 64: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Combining Requests and Responses

SD4x-4.2 64

app.use('/', (req, res) => {

var name = req.query.name; // e.g. /?name=devesh

res.status(200).type('html');

if (name) {

res.write('Hi, ' + name + "it's nice to see you.");

}

else {

res.write('Welcome, guest!');

}

res.end();

});

Page 65: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Combining Requests and Responses

SD4x-4.2 65

app.use('/', (req, res) => {

var name = req.query.name; // e.g. /?name=devesh

res.status(200).type('html');

if (name) {

res.write('Hi, ' + name + "it's nice to see you.");

}

else {

res.write('Welcome, guest!');

}

res.end();

});

Page 66: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Combining Requests and Responses

SD4x-4.2 66

app.use('/', (req, res) => {

var name = req.query.name; // e.g. /?name=devesh

res.status(200).type('html');

if (name) {

res.write('Hi, ' + name + "it's nice to see you.");

}

else {

res.write('Welcome, guest!');

}

res.end();

});

Page 67: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Combining Requests and Responses

SD4x-4.2 67

app.use('/', (req, res) => {

var name = req.query.name; // e.g. /?name=devesh

res.status(200).type('html');

if (name) {

res.write('Hi, ' + name + "it's nice to see you.");

}

else {

res.write('Welcome, guest!');

}

res.end();

});

Page 68: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Combining Requests and Responses

SD4x-4.2 68

app.use('/', (req, res) => {

var name = req.query.name; // e.g. /?name=devesh

res.status(200).type('html');

if (name) {

res.write('Hi, ' + name + "it's nice to see you.");

}

else {

res.write('Welcome, guest!');

}

res.end();

});

Page 69: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Combining Requests and Responses

SD4x-4.2 69

app.use('/', (req, res) => {

var name = req.query.name; // e.g. /?name=devesh

res.status(200).type('html');

if (name) {

res.write('Hi, ' + name + "it's nice to see you.");

}

else {

res.write('Welcome, guest!');

}

res.end();

});

Page 70: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Combining Requests and Responses

SD4x-4.2 70

app.use('/', (req, res) => {

var name = req.query.name; // e.g. /?name=devesh

res.status(200).type('html');

if (name) {

res.write('Hi, ' + name + "it's nice to see you.");

}

else {

res.write('Welcome, guest!');

}

res.end();

});

Page 71: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Combining Requests and Responses

SD4x-4.2 71

app.use('/', (req, res) => {

var name = req.query.name; // e.g. /?name=devesh

res.status(200).type('html');

if (name) {

res.write('Hi, ' + name + "it's nice to see you.");

}

else {

res.write('Welcome, guest!');

}

res.end();

});

Page 72: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Combining Requests and Responses

SD4x-4.2 72

app.use('/', (req, res) => {

var name = req.query.name; // e.g. /?name=devesh

res.status(200).type('html');

if (name) {

res.write('Hi, ' + name + "it's nice to see you.");

}

else {

res.write('Welcome, guest!');

}

res.end();

});

Page 73: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Combining Requests and Responses

SD4x-4.2 73

app.use('/', (req, res) => {

var name = req.query.name; // e.g. /?name=devesh

res.status(200).type('html');

if (name) {

res.write('Hi, ' + name + "it's nice to see you.");

}

else {

res.write('Welcome, guest!');

}

res.end();

});

Page 74: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Summary

• Node.js and Express represent HTTP requests and responses using JavaScript objects

• We can use these objects’ properties and functions to dynamically generate the content that is sent in response to a request

SD4x-4.2 74

Page 75: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Video 4.3

Express Routing

Chris Murphy

SD4x-4.3 75

Page 76: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Review

• Node.js and Express represent HTTP requests and responses using JavaScript objects

• We can use these objects’ properties and functions to dynamically generate the content that is sent in response to a request

SD4x-4.3 76

Page 77: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Routing

var express = require('express');

var app = express();

app.use('/about', (req, res) => {

res.send('This is the about page.');

});

app.use('/login', (req, res) => {

res.send('This is the login page.');

});

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 77

Page 78: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Routing

var express = require('express');

var app = express();

app.use('/about', (req, res) => {

res.send('This is the about page.');

});

app.use('/login', (req, res) => {

res.send('This is the login page.');

});

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 78

Page 79: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Routing

var express = require('express');

var app = express();

app.use('/about', (req, res) => {

res.send('This is the about page.');

});

app.use('/login', (req, res) => {

res.send('This is the login page.');

});

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 79

Page 80: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Routing

var express = require('express');

var app = express();

app.use('/about', (req, res) => {

res.send('This is the about page.');

});

app.use('/login', (req, res) => {

res.send('This is the login page.');

});

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 80

Page 81: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Routing

var express = require('express');

var app = express();

app.use('/about', (req, res) => {

res.send('This is the about page.');

});

app.use('/login', (req, res) => {

res.send('This is the login page.');

});

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 81

Page 82: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Routing

var express = require('express');

var app = express();

app.use('/about', (req, res) => {

res.send('This is the about page.');

});

app.use('/login', (req, res) => {

res.send('This is the login page.');

});

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 82

Page 83: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Routing

var express = require('express');

var app = express();

app.use('/about', (req, res) => {

res.send('This is the about page.');

});

app.use('/login', (req, res) => {

res.send('This is the login page.');

});

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 83

Page 84: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Routing

var express = require('express');

var app = express();

app.use('/about', (req, res) => {

res.send('This is the about page.');

});

app.use('/login', (req, res) => {

res.send('This is the login page.');

});

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 84

Page 85: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Routing

var express = require('express');

var app = express();

app.use('/about', (req, res) => {

res.send('This is the about page.');

});

app.use('/login', (req, res) => {

res.send('This is the login page.');

});

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 85

Page 86: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Routing

var express = require('express');

var app = express();

app.use('/about', (req, res) => {

res.send('This is the about page.');

});

app.use('/login', (req, res) => {

res.send('This is the login page.');

});

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 86

Page 87: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Routing

var express = require('express');

var app = express();

app.use('/about', (req, res) => {

res.send('This is the about page.');

});

app.use('/login', (req, res) => {

res.send('This is the login page.');

});

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 87

Page 88: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Routing

var express = require('express');

var app = express();

app.use('/about', (req, res) => {

res.send('This is the about page.');

});

app.use('/login', (req, res) => {

res.send('This is the login page.');

});

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 88

Page 89: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Routing

var express = require('express');

var app = express();

app.use('/about', (req, res) => {

res.send('This is the about page.');

});

app.use('/login', (req, res) => {

res.send('This is the login page.');

});

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 89

Page 90: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Express Middleware

• A middleware is a function that is invoked in the handling of an HTTP request

• It is used in the “middle” between receiving a request and sending a response

• Multiple middlewares can be chained together on the same request

SD4x-4.3 90

Page 91: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Middleware: Serving Static Files

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 91

• The simplest middleware is express.static, which serves static files that are locally stored

Page 92: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Middleware: Serving Static Files

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 92

• The simplest middleware is express.static, which serves static files that are locally stored

Page 93: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Middleware: Serving Static Files

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 93

• The simplest middleware is express.static, which serves static files that are locally stored

Page 94: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Middleware: Serving Static Files

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 94

• The simplest middleware is express.static, which serves static files that are locally stored

Page 95: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Middleware: Serving Static Files

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 95

• The simplest middleware is express.static, which serves static files that are locally stored

Page 96: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Middleware: Serving Static Files

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 96

• The simplest middleware is express.static, which serves static files that are locally stored

Page 97: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 97

<!-- This is files/index.html -->

<html>

<body>

<h1>Hello!</h1>

<img src="images/kitty.jpg">

<!-- File is files/images/kitty.jpg -->

</body>

</html>

Page 98: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 98

<!-- This is files/index.html -->

<html>

<body>

<h1>Hello!</h1>

<img src="images/kitty.jpg">

<!-- File is files/images/kitty.jpg -->

</body>

</html>

Page 99: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 99

<!-- This is files/index.html -->

<html>

<body>

<h1>Hello!</h1>

<img src="images/kitty.jpg">

<!-- File is files/images/kitty.jpg -->

</body>

</html>

Page 100: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 100

<!-- This is files/index.html -->

<html>

<body>

<h1>Hello!</h1>

<img src="images/kitty.jpg">

<!-- File is files/images/kitty.jpg -->

</body>

</html>

Page 101: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 101

<!-- This is files/index.html -->

<html>

<body>

<h1>Hello!</h1>

<img src="images/kitty.jpg">

<!-- File is files/images/kitty.jpg -->

</body>

</html>

Page 102: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 102

<!-- This is files/index.html -->

<html>

<body>

<h1>Hello!</h1>

<img src="images/kitty.jpg">

<!-- File is files/images/kitty.jpg -->

</body>

</html>

Page 103: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 103

<!-- This is files/index.html -->

<html>

<body>

<h1>Hello!</h1>

<img src="images/kitty.jpg">

<!-- File is files/images/kitty.jpg -->

</body>

</html>

Page 104: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Middleware: Serving Static Files

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 104

• We can use the response object to send back specific HTML files as needed

Page 105: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Middleware: Serving Static Files

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 105

• We can use the response object to send back specific HTML files as needed

Page 106: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Middleware: Serving Static Files

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).send('Not found!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 106

• We can use the response object to send back specific HTML files as needed

Page 107: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Middleware: Serving Static Files

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).sendFile(__dirname + '/404.html');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 107

• We can use the response object to send back specific HTML files as needed

Page 108: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Middleware: Serving Static Files

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).sendFile(__dirname + '/404.html');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 108

• We can use the response object to send back specific HTML files as needed

Page 109: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Middleware: Serving Static Files

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).sendFile(__dirname + '/404.html');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 109

• We can use the response object to send back specific HTML files as needed

Page 110: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Middleware: Serving Static Files

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).sendFile(__dirname + '/404.html');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.3 110

• We can use the response object to send back specific HTML files as needed

Page 111: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Defining and Using Middleware

• Middleware functions can contain any amount of JavaScript code with any functionality

• They take three parameters: req, res, and next

• next() must be called at the end of the function to invoke the next middleware or the final response

SD4x-4.3 111

Page 112: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 112

var express = require('express');

var app = express();

var logger = (req, res, next) => {

var url = req.url;

var time = new Date();

console.log('Received request for ' + url +

' at ' + time);

next();

};

app.use(logger);

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).sendFile(__dirname + '/404.html');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 113: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 113

var express = require('express');

var app = express();

var logger = (req, res, next) => {

var url = req.url;

var time = new Date();

console.log('Received request for ' + url +

' at ' + time);

next();

};

app.use(logger);

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).sendFile(__dirname + '/404.html');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 114: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 114

var express = require('express');

var app = express();

var logger = (req, res, next) => {

var url = req.url;

var time = new Date();

console.log('Received request for ' + url +

' at ' + time);

next();

};

app.use(logger);

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).sendFile(__dirname + '/404.html');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 115: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 115

var express = require('express');

var app = express();

var logger = (req, res, next) => {

var url = req.url;

var time = new Date();

console.log('Received request for ' + url +

' at ' + time);

next();

};

app.use(logger);

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).sendFile(__dirname + '/404.html');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 116: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 116

var express = require('express');

var app = express();

var logger = (req, res, next) => {

var url = req.url;

var time = new Date();

console.log('Received request for ' + url +

' at ' + time);

next();

};

app.use(logger);

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).sendFile(__dirname + '/404.html');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 117: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 117

var express = require('express');

var app = express();

var logger = (req, res, next) => {

var url = req.url;

var time = new Date();

console.log('Received request for ' + url +

' at ' + time);

next();

};

app.use(logger);

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).sendFile(__dirname + '/404.html');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 118: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 118

var express = require('express');

var app = express();

var logger = (req, res, next) => {

var url = req.url;

var time = new Date();

console.log('Received request for ' + url +

' at ' + time);

next();

};

app.use(logger);

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).sendFile(__dirname + '/404.html');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 119: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 119

var express = require('express');

var app = express();

var logger = (req, res, next) => {

var url = req.url;

var time = new Date();

console.log('Received request for ' + url +

' at ' + time);

next();

};

app.use(logger);

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).sendFile(__dirname + '/404.html');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 120: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 120

var express = require('express');

var app = express();

var logger = (req, res, next) => {

var url = req.url;

var time = new Date();

console.log('Received request for ' + url +

' at ' + time);

next();

};

app.use(logger);

app.use('/public', express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).sendFile(__dirname + '/404.html');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 121: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 121

var express = require('express');

var app = express();

var logger = (req, res, next) => {

var url = req.url;

var time = new Date();

console.log('Received request for ' + url +

' at ' + time);

next();

};

// app.use(logger);

app.use('/public', logger, express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).sendFile(__dirname + '/404.html');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 122: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 122

var express = require('express');

var app = express();

var logger = (req, res, next) => {

var url = req.url;

var time = new Date();

console.log('Received request for ' + url +

' at ' + time);

next();

};

// app.use(logger);

app.use('/public', logger, express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).sendFile(__dirname + '/404.html');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 123: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 123

var express = require('express');

var app = express();

var logger = (req, res, next) => {

var url = req.url;

var time = new Date();

console.log('Received request for ' + url +

' at ' + time);

next();

};

// app.use(logger);

app.use('/public', logger, express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).sendFile(__dirname + '/404.html');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 124: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 124

var express = require('express');

var app = express();

var logger = (req, res, next) => {

var url = req.url;

var time = new Date();

console.log('Received request for ' + url +

' at ' + time);

next();

};

// app.use(logger);

app.use('/public', logger, express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).sendFile(__dirname + '/404.html');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 125: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 125

var express = require('express');

var app = express();

var logger = (req, res, next) => {

var url = req.url;

var time = new Date();

console.log('Received request for ' + url +

' at ' + time);

next();

};

// app.use(logger);

app.use('/public', logger, express.static('files'));

app.use( /*default*/ (req, res) => {

res.status(404).sendFile(__dirname + '/404.html');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

Page 126: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Middleware Chaining

• Middleware functions are called in the order in which they are specified

• Each uses the same Request and Response objects

• A middleware function can modify the Request so that it can then be used by subsequent middleware functions “downstream” in the route

SD4x-4.3 126

Page 127: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 127

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 128: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 128

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 129: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 129

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 130: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 130

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 131: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 131

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 132: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 132

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 133: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 133

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 134: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 134

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 135: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 135

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 136: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 136

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 137: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 137

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 138: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 138

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 139: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 139

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 140: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 140

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 141: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 141

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 142: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 142

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 143: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 143

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 144: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 144

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 145: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 145

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 146: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 146

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 147: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 147

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 148: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 148

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 149: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 149

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 150: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 150

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 151: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 151

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 152: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 152

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 153: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 153

var nameFinder = (req, res, next) => {

var name = req.query.name;

if (name) req.username = name.toUpperCase();

else req.username = 'Guest';

next();

}

var greeter = (req, res, next) => {

res.status(200).type('html');

res.write('Hello, ' + req.username);

next();

}

var adminName = (req, res, next) => {

req.username = 'Admin';

next();

}

app.use('/welcome', nameFinder, greeter,

(req, res) => { res.end(); } );

app.use('/admin', adminName, greeter,

(req, res) => { res.end(); } );

Page 154: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Middleware, Routes, and Routers

• We may find that the same combinations of middleware functions are being used in multiple routes

• We can combine middleware functions into “sub-routes” using Routers and then use those in our routes

SD4x-4.3 154

Page 155: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 155

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 156: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 156

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 157: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 157

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 158: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 158

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 159: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 159

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 160: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 160

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 161: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 161

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 162: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 162

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 163: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 163

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 164: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 164

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 165: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 165

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 166: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 166

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 167: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 167

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 168: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 168

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 169: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 169

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 170: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 170

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 171: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 171

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 172: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 172

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 173: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 173

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

var commonRoute = express.Router();

commonRoute.use(header, greeter, footer);

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 174: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 174

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

var commonRoute = express.Router();

commonRoute.use(header, greeter, footer);

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 175: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 175

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

var commonRoute = express.Router();

commonRoute.use(header, greeter, footer);

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 176: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 176

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

var commonRoute = express.Router();

commonRoute.use(header, greeter, footer);

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 177: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 177

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

var commonRoute = express.Router();

commonRoute.use(header, greeter, footer);

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 178: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 178

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

var commonRoute = express.Router();

commonRoute.use(header, greeter, footer);

app.use('/welcome', logger, nameFinder, header,

greeter, footer, (req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 179: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 179

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

var commonRoute = express.Router();

commonRoute.use(header, greeter, footer);

app.use('/welcome', logger, nameFinder, commonRoute,

(req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 180: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 180

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

var commonRoute = express.Router();

commonRoute.use(header, greeter, footer);

app.use('/welcome', logger, nameFinder, commonRoute,

(req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 181: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 181

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

var commonRoute = express.Router();

commonRoute.use(header, greeter, footer);

app.use('/welcome', logger, nameFinder, commonRoute,

(req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 182: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 182

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

var commonRoute = express.Router();

commonRoute.use(header, greeter, footer);

app.use('/welcome', logger, nameFinder, commonRoute,

(req, res) => { res.end(); } );

app.use('/admin', logger, adminName, header,

greeter, footer, (req, res) => { res.end(); } );

Page 183: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.3 183

var nameFinder = (req, res, next) => { . . . }

var greeter = (req, res, next) => { . . . }

var adminName = (req, res, next) => { . . . }

var logger = (req, res, next) => { . . . }

var header = (req, res, next) => { . . . }

var footer = (req, res, next) => { . . . }

var commonRoute = express.Router();

commonRoute.use(header, greeter, footer);

app.use('/welcome', logger, nameFinder, commonRoute,

(req, res) => { res.end(); } );

app.use('/admin', logger, adminName, commonRoute,

(req, res) => { res.end(); } );

Page 184: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Summary

• Routing allows us to specify different functionality for different HTTP requests

• Routing uses middleware functions, each of which handles a different part of the functionality

• Middleware functions can be chained together and can pass values to each other by modifying the Request object

• Routers allow us to combine middleware functions into common “sub-routes”

SD4x-4.3 184

Page 185: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Video 4.4

Getting User Data

Chris Murphy

SD4x-4.4 185

Page 186: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Review

• Node.js and Express allow us to build server-side web apps in JavaScript

• HTTP Requests and Responses are represented as JavaScript objects

• We can display content based on different routes through the app

SD4x-4.4 186

Page 187: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Getting Data from Users: HTTP Requests

• Query parameters

• Key/value pairs that are part of the URL

• Can be part of a static URL or be generated by an HTML form using the “GET” method

• POST data

• Key/value pairs that are included in the body of the HTTP request

• Result from an HTML form using the “POST” method

SD4x-4.4 187

Page 188: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Query Properties

• An HTTP Request object can include queryproperties that come from the URL

http://localhost:3000/?name=Lydia&location=United+States

SD4x-4.4 188

app.use('/', (req, res) => {

var query = req.query;

console.log(query);

var name = query.name; // 'Lydia'

var location = query.location; // 'United States'

var length = Object.keys(query).length; // 2

res.send('Hello World!');

});

Page 189: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Query Properties

• An HTTP Request object can include queryproperties that come from the URL

http://localhost:3000/?name=Lydia&location=United+States

SD4x-4.4 189

app.use('/', (req, res) => {

var query = req.query;

console.log(query);

var name = query.name; // 'Lydia'

var location = query.location; // 'United States'

var length = Object.keys(query).length; // 2

res.send('Hello World!');

});

Page 190: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Query Properties

• An HTTP Request object can include queryproperties that come from the URL

http://localhost:3000/?name=Lydia&location=United+States

SD4x-4.4 190

app.use('/', (req, res) => {

var query = req.query;

console.log(query);

var name = query.name; // 'Lydia'

var location = query.location; // 'United States'

var length = Object.keys(query).length; // 2

res.send('Hello World!');

});

Page 191: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Query Properties

• An HTTP Request object can include queryproperties that come from the URL

http://localhost:3000/?name=Lydia&location=United+States

SD4x-4.4 191

app.use('/', (req, res) => {

var query = req.query;

console.log(query);

var name = query.name; // 'Lydia'

var location = query.location; // 'United States'

var length = Object.keys(query).length; // 2

res.send('Hello World!');

});

Page 192: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Query Properties

• An HTTP Request object can include queryproperties that come from the URL

http://localhost:3000/?name=Lydia&location=United+States

SD4x-4.4 192

app.use('/', (req, res) => {

var query = req.query;

console.log(query);

var name = query.name; // 'Lydia'

var location = query.location; // 'United States'

var length = Object.keys(query).length; // 2

res.send('Hello World!');

});

Page 193: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Query Properties

• An HTTP Request object can include queryproperties that come from the URL

http://localhost:3000/?name=Lydia&location=United+States

SD4x-4.4 193

app.use('/', (req, res) => {

var query = req.query;

console.log(query);

var name = query.name; // 'Lydia'

var location = query.location; // 'United States'

var length = Object.keys(query).length; // 2

res.send('Hello World!');

});

Page 194: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Query Properties

• An HTTP Request object can include queryproperties that come from the URL

http://localhost:3000/?name=Lydia&location=United+States

SD4x-4.4 194

app.use('/', (req, res) => {

var query = req.query;

console.log(query);

var name = query.name; // 'Lydia'

var location = query.location; // 'United States'

var length = Object.keys(query).length; // 2

res.send('Hello World!');

});

Page 195: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Query Properties

• An HTTP Request object can include queryproperties that come from the URL

http://localhost:3000/?name=Lydia&location=United+States

SD4x-4.4 195

app.use('/', (req, res) => {

var query = req.query;

console.log(query);

var name = query.name; // 'Lydia'

var location = query.location; // 'United States'

var length = Object.keys(query).length; // 2

res.send('Hello World!');

});

Page 196: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Query Properties

• An HTTP Request object can include queryproperties that come from the URL

http://localhost:3000/?name=Lydia&location=United+States

SD4x-4.4 196

app.use('/', (req, res) => {

var query = req.query;

console.log(query);

var name = query.name; // 'Lydia'

var location = query.location; // 'United States'

var length = Object.keys(query).length; // 2

res.send('Hello World!');

});

Page 197: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Query Properties

• An HTTP Request object can include queryproperties that come from the URL

http://localhost:3000/?name=Lydia&location=United+States

SD4x-4.4 197

app.use('/', (req, res) => {

var query = req.query;

console.log(query);

var name = query.name; // 'Lydia'

var location = query.location; // 'United States'

var length = Object.keys(query).length; // 2

res.send('Hello World!');

});

Page 198: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Query Properties

• An HTTP Request object can include queryproperties that come from the URL

http://localhost:3000/?name=Lydia&location=United+States

SD4x-4.4 198

app.use('/', (req, res) => {

var query = req.query;

console.log(query);

var name = query.name; // 'Lydia'

var location = query.location; // 'United States'

var length = Object.keys(query).length; // 2

res.send('Hello World!');

});

Page 199: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Query Properties

• An HTTP Request object can include queryproperties that come from the URL

http://localhost:3000/?name=Lydia&location=United+States

SD4x-4.4 199

app.use('/', (req, res) => {

var query = req.query;

console.log(query);

var name = query.name; // 'Lydia'

var location = query.location; // 'United States'

var length = Object.keys(query).length; // 2

res.send('Hello World!');

});

Page 200: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Query Properties

• An HTTP Request object can include queryproperties that come from the URL

http://localhost:3000/?name=Lydia&location=United+States

SD4x-4.4 200

app.use('/', (req, res) => {

var query = req.query;

console.log(query);

var name = query.name; // 'Lydia'

var location = query.location; // 'United States'

var length = Object.keys(query).length; // 2

res.send('Hello World!');

});

Page 201: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Query Properties

• An HTTP Request object can include queryproperties that come from the URL

http://localhost:3000/?name=Lydia&location=United+States

SD4x-4.4 201

app.use('/', (req, res) => {

var query = req.query;

console.log(query);

var name = query.name; // 'Lydia'

var location = query.location; // 'United States'

var length = Object.keys(query).length; // 2

res.send('Hello World!');

});

Page 202: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Parameters

• An HTTP Request object can include paramproperties that come from a parameterized URL

http://localhost:3000/name/Lydia/location/United States

SD4x-4.4 202

app.use('/name/:userName/location/:userLocation',

(req, res) => {

var params = req.params;

console.log(params);

var name = params.userName; // 'Lydia'

var location = params.userLocation; // 'United States'

var length = Object.keys(params).length; // 2

res.send('Hello World!');

});

Page 203: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Parameters

• An HTTP Request object can include paramproperties that come from a parameterized URL

http://localhost:3000/name/Lydia/location/United States

SD4x-4.4 203

app.use('/name/:userName/location/:userLocation',

(req, res) => {

var params = req.params;

console.log(params);

var name = params.userName; // 'Lydia'

var location = params.userLocation; // 'United States'

var length = Object.keys(params).length; // 2

res.send('Hello World!');

});

Page 204: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Parameters

• An HTTP Request object can include paramproperties that come from a parameterized URL

http://localhost:3000/name/Lydia/location/United States

SD4x-4.4 204

app.use('/name/:userName/location/:userLocation',

(req, res) => {

var params = req.params;

console.log(params);

var name = params.userName; // 'Lydia'

var location = params.userLocation; // 'United States'

var length = Object.keys(params).length; // 2

res.send('Hello World!');

});

Page 205: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Parameters

• An HTTP Request object can include paramproperties that come from a parameterized URL

http://localhost:3000/name/Lydia/location/United States

SD4x-4.4 205

app.use('/name/:userName/location/:userLocation',

(req, res) => {

var params = req.params;

console.log(params);

var name = params.userName; // 'Lydia'

var location = params.userLocation; // 'United States'

var length = Object.keys(params).length; // 2

res.send('Hello World!');

});

Page 206: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Parameters

• An HTTP Request object can include paramproperties that come from a parameterized URL

http://localhost:3000/name/Lydia/location/United States

SD4x-4.4 206

app.use('/name/:userName/location/:userLocation',

(req, res) => {

var params = req.params;

console.log(params);

var name = params.userName; // 'Lydia'

var location = params.userLocation; // 'United States'

var length = Object.keys(params).length; // 2

res.send('Hello World!');

});

Page 207: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Parameters

• An HTTP Request object can include paramproperties that come from a parameterized URL

http://localhost:3000/name/Lydia/location/United States

SD4x-4.4 207

app.use('/name/:userName/location/:userLocation',

(req, res) => {

var params = req.params;

console.log(params);

var name = params.userName; // 'Lydia'

var location = params.userLocation; // 'United States'

var length = Object.keys(params).length; // 2

res.send('Hello World!');

});

Page 208: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Parameters

• An HTTP Request object can include paramproperties that come from a parameterized URL

http://localhost:3000/name/Lydia/location/United States

SD4x-4.4 208

app.use('/name/:userName/location/:userLocation',

(req, res) => {

var params = req.params;

console.log(params);

var name = params.userName; // 'Lydia'

var location = params.userLocation; // 'United States'

var length = Object.keys(params).length; // 2

res.send('Hello World!');

});

Page 209: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Parameters

• An HTTP Request object can include paramproperties that come from a parameterized URL

http://localhost:3000/name/Lydia/location/United States

SD4x-4.4 209

app.use('/name/:userName/location/:userLocation',

(req, res) => {

var params = req.params;

console.log(params);

var name = params.userName; // 'Lydia'

var location = params.userLocation; // 'United States'

var length = Object.keys(params).length; // 2

res.send('Hello World!');

});

Page 210: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Parameters

• An HTTP Request object can include paramproperties that come from a parameterized URL

http://localhost:3000/name/Lydia/location/United States

SD4x-4.4 210

app.use('/name/:userName/location/:userLocation',

(req, res) => {

var params = req.params;

console.log(params);

var name = params.userName; // 'Lydia'

var location = params.userLocation; // 'United States'

var length = Object.keys(params).length; // 2

res.send('Hello World!');

});

Page 211: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Parameters

• An HTTP Request object can include paramproperties that come from a parameterized URL

http://localhost:3000/name/Lydia/location/United States

SD4x-4.4 211

app.use('/name/:userName/location/:userLocation',

(req, res) => {

var params = req.params;

console.log(params);

var name = params.userName; // 'Lydia'

var location = params.userLocation; // 'United States'

var length = Object.keys(params).length; // 2

res.send('Hello World!');

});

Page 212: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Parameters

• An HTTP Request object can include paramproperties that come from a parameterized URL

http://localhost:3000/name/Lydia/location/United States

SD4x-4.4 212

app.use('/name/:userName/location/:userLocation',

(req, res) => {

var params = req.params;

console.log(params);

var name = params.userName; // 'Lydia'

var location = params.userLocation; // 'United States'

var length = Object.keys(params).length; // 2

res.send('Hello World!');

});

Page 213: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Parameters

• An HTTP Request object can include paramproperties that come from a parameterized URL

http://localhost:3000/name/Lydia/location/United States

SD4x-4.4 213

app.use('/name/:userName/location/:userLocation',

(req, res) => {

var params = req.params;

console.log(params);

var name = params.userName; // 'Lydia'

var location = params.userLocation; // 'United States'

var length = Object.keys(params).length; // 2

res.send('Hello World!');

});

Page 214: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Parameters

• An HTTP Request object can include paramproperties that come from a parameterized URL

http://localhost:3000/name/Lydia/location/United States

SD4x-4.4 214

app.use('/name/:userName/location/:userLocation',

(req, res) => {

var params = req.params;

console.log(params);

var name = params.userName; // 'Lydia'

var location = params.userLocation; // 'United States'

var length = Object.keys(params).length; // 2

res.send('Hello World!');

});

Page 215: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Parameters

• An HTTP Request object can include paramproperties that come from a parameterized URL

http://localhost:3000/name/Lydia/location/United States

SD4x-4.4 215

app.use('/name/:userName/location/:userLocation',

(req, res) => {

var params = req.params;

console.log(params);

var name = params.userName; // 'Lydia'

var location = params.userLocation; // 'United States'

var length = Object.keys(params).length; // 2

res.send('Hello World!');

});

Page 216: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Parameters

• An HTTP Request object can include paramproperties that come from a parameterized URL

http://localhost:3000/name/Lydia/location/United States

SD4x-4.4 216

app.use('/name/:userName/location/:userLocation',

(req, res) => {

var params = req.params;

console.log(params);

var name = params.userName; // 'Lydia'

var location = params.userLocation; // 'United States'

var length = Object.keys(params).length; // 2

res.send('Hello World!');

});

Page 217: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Parameters

• An HTTP Request object can include paramproperties that come from a parameterized URL

http://localhost:3000/name/Lydia/location/United States

SD4x-4.4 217

app.use('/name/:userName/location/:userLocation',

(req, res) => {

var params = req.params;

console.log(params);

var name = params.userName; // 'Lydia'

var location = params.userLocation; // 'United States'

var length = Object.keys(params).length; // 2

res.send('Hello World!');

});

Page 218: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Request Object Parameters

• An HTTP Request object can include paramproperties that come from a parameterized URL

http://localhost:3000/name/Lydia/location/United States

SD4x-4.4 218

app.use('/name/:userName/location/:userLocation',

(req, res) => {

var params = req.params;

console.log(params);

var name = params.userName; // 'Lydia'

var location = params.userLocation; // 'United States'

var length = Object.keys(params).length; // 2

res.send('Hello World!');

});

Page 219: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

HTML Forms

• Forms allow users to enter or select data, e.g. via input boxes, checkboxes, radio buttons, etc.

• The form specifies the action and method that result when the user chooses to submit the form

• Action: the URL to be requested

• Method: the HTTP Request “verb,” e.g. GET or POST

SD4x-4.4 219

Page 220: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.4 220

Page 221: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.4 221

Page 222: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

<html>

<body>

<form action="/handleForm" method="post">

Name: <input name="username">

<p>

I like:<br>

<input type=checkbox name="animal" value="dogs">Dogs <br>

<input type=checkbox name="animal" value="cats">Cats <br>

<input type=checkbox name="animal" value="birds">Birds <br>

<p>

<input type=submit value="Submit form!">

</form>

</body>

</html>

SD4x-4.4 222

Page 223: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

<html>

<body>

<form action="/handleForm" method="post">

Name: <input name="username">

<p>

I like:<br>

<input type=checkbox name="animal" value="dogs">Dogs <br>

<input type=checkbox name="animal" value="cats">Cats <br>

<input type=checkbox name="animal" value="birds">Birds <br>

<p>

<input type=submit value="Submit form!">

</form>

</body>

</html>

SD4x-4.4 223

Page 224: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

<html>

<body>

<form action="/handleForm" method="post">

Name: <input name="username">

<p>

I like:<br>

<input type=checkbox name="animal" value="dogs">Dogs <br>

<input type=checkbox name="animal" value="cats">Cats <br>

<input type=checkbox name="animal" value="birds">Birds <br>

<p>

<input type=submit value="Submit form!">

</form>

</body>

</html>

SD4x-4.4 224

Page 225: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

<html>

<body>

<form action="/handleForm" method="post">

Name: <input name="username">

<p>

I like:<br>

<input type=checkbox name="animal" value="dogs">Dogs <br>

<input type=checkbox name="animal" value="cats">Cats <br>

<input type=checkbox name="animal" value="birds">Birds <br>

<p>

<input type=submit value="Submit form!">

</form>

</body>

</html>

SD4x-4.4 225

Page 226: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

<html>

<body>

<form action="/handleForm" method="post">

Name: <input name="username">

<p>

I like:<br>

<input type=checkbox name="animal" value="dogs">Dogs <br>

<input type=checkbox name="animal" value="cats">Cats <br>

<input type=checkbox name="animal" value="birds">Birds <br>

<p>

<input type=submit value="Submit form!">

</form>

</body>

</html>

SD4x-4.4 226

Page 227: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

<html>

<body>

<form action="/handleForm" method="post">

Name: <input name="username">

<p>

I like:<br>

<input type=checkbox name="animal" value="dogs">Dogs <br>

<input type=checkbox name="animal" value="cats">Cats <br>

<input type=checkbox name="animal" value="birds">Birds <br>

<p>

<input type=submit value="Submit form!">

</form>

</body>

</html>

SD4x-4.4 227

Page 228: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

<html>

<body>

<form action="/handleForm" method="post">

Name: <input name="username">

<p>

I like:<br>

<input type=checkbox name="animal" value="dogs">Dogs <br>

<input type=checkbox name="animal" value="cats">Cats <br>

<input type=checkbox name="animal" value="birds">Birds <br>

<p>

<input type=submit value="Submit form!">

</form>

</body>

</html>

SD4x-4.4 228

Page 229: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

<html>

<body>

<form action="/handleForm" method="post">

Name: <input name="username">

<p>

I like:<br>

<input type=checkbox name="animal" value="dogs">Dogs <br>

<input type=checkbox name="animal" value="cats">Cats <br>

<input type=checkbox name="animal" value="birds">Birds <br>

<p>

<input type=submit value="Submit form!">

</form>

</body>

</html>

SD4x-4.4 229

Page 230: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Reading POST Data in Express

• When a form’s method is “GET”, the data is sent in the URL query parameters

• When a form’s method is “POST”, the data is sent in the body of the HTTP request

• To read the body of the HTTP request in Express, use the body-parser middleware

• To install it, run: npm install body-parser

SD4x-4.4 230

Page 231: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = req.body.animal; // this is an array

. . .

res.send('Thank you!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.4 231

Page 232: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = req.body.animal; // this is an array

. . .

res.send('Thank you!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.4 232

Page 233: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = req.body.animal; // this is an array

. . .

res.send('Thank you!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.4 233

Page 234: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = req.body.animal; // this is an array

. . .

res.send('Thank you!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.4 234

Page 235: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = req.body.animal; // this is an array

. . .

res.send('Thank you!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.4 235

Page 236: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = req.body.animal; // this is an array

. . .

res.send('Thank you!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.4 236

Page 237: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = req.body.animal; // this is an array

. . .

res.send('Thank you!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.4 237

Page 238: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

<html>

<body>

<form action="/handleForm" method="post">

Name: <input name="username">

<p>

I like:<br>

<input type=checkbox name="animal" value="dogs">Dogs <br>

<input type=checkbox name="animal" value="cats">Cats <br>

<input type=checkbox name="animal" value="birds">Birds <br>

<p>

<input type=submit value="Submit form!">

</form>

</body>

</html>

SD4x-4.4 238

Page 239: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = req.body.animal; // this is an array

. . .

res.send('Thank you!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.4 239

Page 240: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = req.body.animal; // this is an array

. . .

res.send('Thank you!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.4 240

Page 241: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = req.body.animal; // this is an array

. . .

res.send('Thank you!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.4 241

Page 242: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = req.body.animal; // this is an array

. . .

res.send('Thank you!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.4 242

Page 243: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = req.body.animal; // this is an array

. . .

res.send('Thank you!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.4 243

Page 244: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

<html>

<body>

<form action="/handleForm" method="post">

Name: <input name="username">

<p>

I like:<br>

<input type=checkbox name="animal" value="dogs">Dogs <br>

<input type=checkbox name="animal" value="cats">Cats <br>

<input type=checkbox name="animal" value="birds">Birds <br>

<p>

<input type=submit value="Submit form!">

</form>

</body>

</html>

SD4x-4.4 244

Page 245: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = req.body.animal; // this is an array

. . .

res.send('Thank you!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.4 245

Page 246: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = req.body.animal; // this is an array

. . .

res.send('Thank you!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.4 246

Page 247: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

<html>

<body>

<form action="/handleForm" method="post">

Name: <input name="username">

<p>

I like:<br>

<input type=checkbox name="animal" value="dogs">Dogs <br>

<input type=checkbox name="animal" value="cats">Cats <br>

<input type=checkbox name="animal" value="birds">Birds <br>

<p>

<input type=submit value="Submit form!">

</form>

</body>

</html>

SD4x-4.4 247

Page 248: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

var express = require('express');

var app = express();

app.use('/public', express.static('files'));

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = req.body.animal; // this is an array

. . .

res.send('Thank you!');

});

app.listen(3000, () => {

console.log('Listening on port 3000');

});

SD4x-4.4 248

Page 249: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Summary

• HTTP Request query properties: key/value pairs that come from URL

• HTTP Request param properties: key/value pairs that come from parameterized URL

• HTTP Request body properties: input data from form submitting using POST method

SD4x-4.4 249

Page 250: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Video 4.5

EJS

Chris Murphy

SD4x-4.5 250

Page 251: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Review

• Node.js and Express allow us to build server-side web apps in JavaScript

• HTTP Requests and Responses are represented as JavaScript objects

• We can get data from user either via the URL or through form submissions

SD4x-4.5 251

Page 252: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 252

Page 253: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 253

Page 254: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 254

Page 255: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 255

Page 256: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 256

Page 257: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 257

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 258: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 258

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 259: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 259

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 260: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 260

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 261: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 261

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 262: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 262

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 263: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 263

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 264: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 264

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 265: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 265

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 266: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 266

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 267: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 267

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 268: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 268

Page 269: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 269

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 270: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 270

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 271: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 271

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 272: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 272

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 273: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 273

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 274: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 274

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 275: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 275

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 276: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 276

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 277: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 277

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 278: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 278

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 279: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 279

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 280: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

What is EJS?

• EJS, or EmbeddedJS, is a view engine that uses data and embedded JavaScript to produce HTML

• This allows webpages to be developed statically and rendered dynamically server-side

• EmbeddedJS is a package that can be installed with the command: npm install ejs

SD4x-4.5 280

Page 281: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Using EJS in an Express app

• Set EJS as the default rendering method in your app with app.set('view engine', 'ejs');

var express = require(‘express’);

var app = express();

app.set('view engine', 'ejs');

app.get('/', (req, res) => {

res.render('welcome', {username: 'CandyLover'});

});

SD4x-4.5 281

Page 282: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Using EJS in an Express app

• Set EJS as the default rendering method in your app with app.set('view engine', 'ejs');

var express = require(‘express’);

var app = express();

app.set('view engine', 'ejs');

app.get('/', (req, res) => {

res.render('welcome', {username: 'CandyLover'});

});

SD4x-4.5 282

Page 283: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Using EJS in an Express app

• Set EJS as the default rendering method in your app with app.set('view engine', 'ejs');

• Then generate and send the HTML from an .ejs file using the Response’s render function

var express = require(‘express’);

var app = express();

app.set('view engine', 'ejs');

app.get('/', (req, res) => {

res.render('welcome', {username: 'CandyLover'});

});

SD4x-4.5 283

Page 284: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Using EJS in an Express app

• Set EJS as the default rendering method in your app with app.set('view engine', 'ejs');

• Then generate and send the HTML from an .ejs file using the Response’s render function

• Arguments to the .ejs file are passed as objects

var express = require(‘express’);

var app = express();

app.set('view engine', 'ejs');

app.get('/', (req, res) => {

res.render('welcome', {username: 'CandyLover'});

});

SD4x-4.5 284

Page 285: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Writing EJS files

• A .ejs file is just an HTML file that has JavaScript code embedded in it

• Anything between <%= and %> tags will be evaluated and incorporated into the HTML

• By default, the .ejs files should be in the views/ subdirectory of the Express project

<!-- This is views/welcome.ejs -->

<!DOCTYPE html>

<html>

<body>

<h1>Welcome, <%= username %>!</h1>

</body>

</html>SD4x-4.5 285

Page 286: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Writing EJS files

• A .ejs file is just an HTML file that has JavaScript code embedded in it

• Anything between <%= and %> tags will be evaluated and incorporated into the HTML

• By default, the .ejs files should be in the views/ subdirectory of the Express project

<!-- This is views/welcome.ejs -->

<!DOCTYPE html>

<html>

<body>

<h1>Welcome, <%= username %>!</h1>

</body>

</html>SD4x-4.5 286

Page 287: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Writing EJS files

• A .ejs file is just an HTML file that has JavaScript code embedded in it

• Anything between <%= and %> tags will be evaluated and incorporated into the HTML

• By default, the .ejs files should be in the views/ subdirectory of the Express project

<!-- This is views/welcome.ejs -->

<!DOCTYPE html>

<html>

<body>

<h1>Welcome, <%= username %>!</h1>

</body>

</html>SD4x-4.5 287

Page 288: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Writing EJS files

• A .ejs file is just an HTML file that has JavaScript code embedded in it

• Anything between <%= and %> tags will be evaluated and incorporated into the HTML

• By default, the .ejs files should be in the views/ subdirectory of the Express project

<!-- This is views/welcome.ejs -->

<!DOCTYPE html>

<html>

<body>

<h1>Welcome, <%= username %>!</h1>

</body>

</html>SD4x-4.5 288

Page 289: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

EJS and JavaScript

• EJS will execute any JavaScript that appears between <% and %> tags when generating the HTML page on the server

<!DOCTYPE html>

<html>

<body>

<h1>Welcome, <%= username %>!</h1>

<% if (isAdmin) { %>

<p> Remember to check your email every 24 hours! </p>

<% } %>

</body>

</html>

res.render('welcome', {username: 'CandyLover', isAdmin: true});

SD4x-4.5 289

Page 290: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

EJS and JavaScript

• EJS will execute any JavaScript that appears between <% and %> tags when generating the HTML page on the server

<!DOCTYPE html>

<html>

<body>

<h1>Welcome, <%= username %>!</h1>

<% if (isAdmin) { %>

<p> Remember to check your email every 24 hours! </p>

<% } %>

</body>

</html>

res.render('welcome', {username: 'CandyLover', isAdmin: true});

SD4x-4.5 290

Page 291: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

EJS and JavaScript

• EJS will execute any JavaScript that appears between <% and %> tags when generating the HTML page on the server

<!DOCTYPE html>

<html>

<body>

<h1>Welcome, <%= username %>!</h1>

<% if (isAdmin) { %>

<p> Remember to check your email every 24 hours! </p>

<% } %>

</body>

</html>

res.render('welcome', {username: 'CandyLover', isAdmin: true});

SD4x-4.5 291

Page 292: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

EJS and JavaScript

• EJS will execute any JavaScript that appears between <% and %> tags when generating the HTML page on the server

<!DOCTYPE html>

<html>

<body>

<h1>Welcome, <%= username %>!</h1>

<% if (isAdmin) { %>

<p> Remember to check your email every 24 hours! </p>

<% } %>

</body>

</html>

res.render('welcome', {username: 'CandyLover', isAdmin: true});

SD4x-4.5 292

Page 293: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

EJS and JavaScript

• EJS will execute any JavaScript that appears between <% and %> tags when generating the HTML page on the server

<!DOCTYPE html>

<html>

<body>

<h1>Welcome, <%= username %>!</h1>

<% if (isAdmin) { %>

<p> Remember to check your email every 24 hours! </p>

<% } %>

</body>

</html>

res.render('welcome', {username: 'CandyLover', isAdmin: true});

SD4x-4.5 293

Page 294: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

EJS and JavaScript

• EJS will execute any JavaScript that appears between <% and %> tags when generating the HTML page on the server

<!DOCTYPE html>

<html>

<body>

<h1>Welcome, <%= username %>!</h1>

<% if (isAdmin) { %>

<p> Remember to check your email every 24 hours! </p>

<% } %>

</body>

</html>

res.render('welcome', {username: 'CandyLover', isAdmin: true});

SD4x-4.5 294

Page 295: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

EJS and JavaScript

• EJS will execute any JavaScript that appears between <% and %> tags when generating the HTML page on the server

<!DOCTYPE html>

<html>

<body>

<h1>Welcome, <%= username %>!</h1>

<% if (isAdmin) { %>

<p> Remember to check your email every 24 hours! </p>

<% } %>

</body>

</html>

res.render('welcome', {username: 'CandyLover', isAdmin: true});

SD4x-4.5 295

Page 296: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

EJS and JavaScript

• EJS will execute any JavaScript that appears between <% and %> tags when generating the HTML page on the server

<!DOCTYPE html>

<html>

<body>

<h1>Welcome, <%= username %>!</h1>

<% if (isAdmin) { %>

<p> Remember to check your email every 24 hours! </p>

<% } %>

</body>

</html>

res.render('welcome', {username: 'CandyLover', isAdmin: true});

SD4x-4.5 296

Page 297: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

EJS and JavaScript

• EJS will execute any JavaScript that appears between <% and %> tags when generating the HTML page on the server

<!DOCTYPE html>

<html>

<body>

<h1>Welcome, <%= username %>!</h1>

<% if (isAdmin) { %>

<p> Remember to check your email every 24 hours! </p>

<% } %>

</body>

</html>

res.render('welcome', {username: 'CandyLover', isAdmin: true});

SD4x-4.5 297

Page 298: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

EJS and JavaScript

• EJS will execute any JavaScript that appears between <% and %> tags when generating the HTML page on the server

<!DOCTYPE html>

<html>

<body>

<h1>Welcome, <%= username %>!</h1>

<% if (isAdmin) { %>

<p> Remember to check your email every 24 hours! </p>

<% } %>

</body>

</html>

res.render('welcome', {username: 'CandyLover', isAdmin: true});

SD4x-4.5 298

Page 299: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

EJS and JavaScript

• EJS will execute any JavaScript that appears between <% and %> tags when generating the HTML page on the server

<!DOCTYPE html>

<html>

<body>

<h1>Welcome, <%= username %>!</h1>

<% if (isAdmin) { %>

<p> Remember to check your email every 24 hours! </p>

<% } %>

</body>

</html>

res.render('welcome', {username: 'CandyLover', isAdmin: true});

SD4x-4.5 299

Page 300: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 300

Page 301: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 301

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 302: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 302

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.type('html').status(200);

res.write('Hello, ' + name + ', nice to meet you.');

res.write('<p>Here are the animals you like:');

res.write('<ul>');

animals.forEach( (animal) => {

res.write('<li>' + animal + '</li>');

});

res.write('</ul>');

res.write("<a href='/public/form.html'>" +

"Back to form</a>");

res.end();

});

Page 303: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 303

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

Page 304: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 304

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

Page 305: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 305

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

Page 306: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 306

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

Page 307: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 307

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

Page 308: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 308

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

Page 309: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 309

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

Page 310: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 310

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

<!-- This is views/showAnimals.ejs -->

Hello, <%= name %>, nice to meet you.

<p>Here are the animals you like:

<ul>

<% animals.forEach( (animal) => { %>

<li> <%= animal %> </li>

<% }); %>

</ul>

<a href='/public/form.html'>Back to form</a>

Page 311: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 311

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

<!-- This is views/showAnimals.ejs -->

Hello, <%= name %>, nice to meet you.

<p>Here are the animals you like:

<ul>

<% animals.forEach( (animal) => { %>

<li> <%= animal %> </li>

<% }); %>

</ul>

<a href='/public/form.html'>Back to form</a>

Page 312: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 312

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

<!-- This is views/showAnimals.ejs -->

Hello, <%= name %>, nice to meet you.

<p>Here are the animals you like:

<ul>

<% animals.forEach( (animal) => { %>

<li> <%= animal %> </li>

<% }); %>

</ul>

<a href='/public/form.html'>Back to form</a>

Page 313: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 313

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

<!-- This is views/showAnimals.ejs -->

Hello, <%= name %>, nice to meet you.

<p>Here are the animals you like:

<ul>

<% animals.forEach( (animal) => { %>

<li> <%= animal %> </li>

<% }); %>

</ul>

<a href='/public/form.html'>Back to form</a>

Page 314: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 314

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

<!-- This is views/showAnimals.ejs -->

Hello, <%= name %>, nice to meet you.

<p>Here are the animals you like:

<ul>

<% animals.forEach( (animal) => { %>

<li> <%= animal %> </li>

<% }); %>

</ul>

<a href='/public/form.html'>Back to form</a>

Page 315: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 315

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

<!-- This is views/showAnimals.ejs -->

Hello, <%= name %>, nice to meet you.

<p>Here are the animals you like:

<ul>

<% animals.forEach( (animal) => { %>

<li> <%= animal %> </li>

<% }); %>

</ul>

<a href='/public/form.html'>Back to form</a>

Page 316: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 316

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

<!-- This is views/showAnimals.ejs -->

Hello, <%= name %>, nice to meet you.

<p>Here are the animals you like:

<ul>

<% animals.forEach( (animal) => { %>

<li> <%= animal %> </li>

<% }); %>

</ul>

<a href='/public/form.html'>Back to form</a>

Page 317: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 317

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

<!-- This is views/showAnimals.ejs -->

Hello, <%= name %>, nice to meet you.

<p>Here are the animals you like:

<ul>

<% animals.forEach( (animal) => { %>

<li> <%= animal %> </li>

<% }); %>

</ul>

<a href='/public/form.html'>Back to form</a>

Page 318: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 318

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

<!-- This is views/showAnimals.ejs -->

Hello, <%= name %>, nice to meet you.

<p>Here are the animals you like:

<ul>

<% animals.forEach( (animal) => { %>

<li> <%= animal %> </li>

<% }); %>

</ul>

<a href='/public/form.html'>Back to form</a>

Page 319: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 319

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

<!-- This is views/showAnimals.ejs -->

Hello, <%= name %>, nice to meet you.

<p>Here are the animals you like:

<ul>

<% animals.forEach( (animal) => { %>

<li> <%= animal %> </li>

<% }); %>

</ul>

<a href='/public/form.html'>Back to form</a>

Page 320: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 320

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

<!-- This is views/showAnimals.ejs -->

Hello, <%= name %>, nice to meet you.

<p>Here are the animals you like:

<ul>

<% animals.forEach( (animal) => { %>

<li> <%= animal %> </li>

<% }); %>

</ul>

<a href='/public/form.html'>Back to form</a>

Page 321: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 321

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

<!-- This is views/showAnimals.ejs -->

Hello, <%= name %>, nice to meet you.

<p>Here are the animals you like:

<ul>

<% animals.forEach( (animal) => { %>

<li> <%= animal %> </li>

<% }); %>

</ul>

<a href='/public/form.html'>Back to form</a>

Page 322: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 322

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

<!-- This is views/showAnimals.ejs -->

Hello, <%= name %>, nice to meet you.

<p>Here are the animals you like:

<ul>

<% animals.forEach( (animal) => { %>

<li> <%= animal %> </li>

<% }); %>

</ul>

<a href='/public/form.html'>Back to form</a>

Page 323: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.5 323

app.use('/handleForm', (req, res) => {

var name = req.body.username;

var animals = [].concat(req.body.animal);

res.render('showAnimals', { name: name,

animals: animals });

});

<!-- This is views/showAnimals.ejs -->

Hello, <%= name %>, nice to meet you.

<p>Here are the animals you like:

<ul>

<% animals.forEach( (animal) => { %>

<li> <%= animal %> </li>

<% }); %>

</ul>

<a href='/public/form.html'>Back to form</a>

Page 324: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Summary

• EJS allows webpages to be developed statically and rendered dynamically server-side

• An Express app can generate and send the HTML from a .ejs file using the Response’s renderfunction

• EJS will execute any JavaScript that appears between <% and %> tags when generating the HTML page on the server

SD4x-4.5 324

Page 325: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Video 4.6

MongoDB: Setup and Insert

Chris Murphy

SD4x-4.6 325

Page 326: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Review

• Node.js and Express allow us to build server-side web apps in JavaScript

• We can get data from the user via the URL query parameters or from form data

SD4x-4.6 326

Page 327: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 327

Client Server

Page 328: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 328

Client Server Database

Page 329: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

What is a NoSQL Database?

• A NoSQL Database is a database that does not use SQL, the traditional method of storing data

• In SQL, data is stored in tables and rows. This is also known as a relational database

• NoSQL Databases attempt to address some of the shortcomings of SQL and other relational databases by organizing and storing data differently

SD4x-4.6 329

Page 330: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

What is MongoDB?

• MongoDB is one NoSQL Database that is designed for use with JavaScript apps

• MongoDB stores collections of documents rather than tables of rows

SD4x-4.6 330

Page 331: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

SQL Database

• A SQL table of Users might look like this:

Name Age Country Occupation

Jane Doe 30 United States Programmer

John Smith 25 Canada Doctor

Kim Jones 27 France Painter

SD4x-4.6 331

Page 332: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

MongoDB Documents

• A MongoDB collection of User documents might look like this:

{

name: 'Jane Doe',

age: 30,

country: 'United States',

occupation: 'Programmer'

}

{

name: 'John Smith',

age: 25,

country: 'Canada',

occupation: 'Doctor'

}

{

name: 'Kim Jones',

age: 27,

country: 'France',

occupation: 'Painter'

}

SD4x-4.6 332

Page 333: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Using MongoDB with a Node.js app

1. Install MongoDB locally or create an account on a remote service

2. Install packages locally to allow your JavaScript programs to access your MongoDB instance

3. Write JavaScript to describe the Schema (blueprint for Documents) that you will use in the Collection

4. Use the Schema to access MongoDB in your app

SD4x-4.6 333

Page 334: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Installing MongoDB

• You can find download/installation instructions for MongoDB at https://mongodb.com/download

• Follow these instructions to create a new empty database and run the MongoDB server

• When you start it, it will tell you which port it is using

SD4x-4.6 334

Page 335: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 335

Page 336: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 336

Page 337: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 337

Page 338: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Installing MongoDB

• You can find download/installation instructions for MongoDB at https://mongodb.com/download

• Follow these instructions to create a new empty database and run the MongoDB server

• When you start it, it will tell you which port it is using

SD4x-4.6 338

Page 339: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Installing MongoDB

• You can find download/installation instructions for MongoDB at https://mongodb.com/download

• Follow these instructions to create a new empty database and run the MongoDB server

• When you start it, it will tell you which port it is using

• Alternatively, you may use an online service, e.g. MongoDB Atlas

SD4x-4.6 339

Page 340: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Installing Drivers for MongoDB

• You can access MongoDB directly from your Node app using the MongoClient

• Alternatively, you can install helper packages such as Mongoose to simplify some tasks:

npm install mongoose --save

SD4x-4.6 340

Page 341: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 341

Page 342: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 342

<html>

<body>

<form action='/create' method='post'>

Name: <input name='name'>

<p>

Age: <input name='age'>

<p>

<input type=submit value='Submit form!'>

</form>

</body>

</html>

Page 343: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 343

<html>

<body>

<form action='/create' method='post'>

Name: <input name='name'>

<p>

Age: <input name='age'>

<p>

<input type=submit value='Submit form!'>

</form>

</body>

</html>

Page 344: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 344

<html>

<body>

<form action='/create' method='post'>

Name: <input name='name'>

<p>

Age: <input name='age'>

<p>

<input type=submit value='Submit form!'>

</form>

</body>

</html>

Page 345: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 345

<html>

<body>

<form action='/create' method='post'>

Name: <input name='name'>

<p>

Age: <input name='age'>

<p>

<input type=submit value='Submit form!'>

</form>

</body>

</html>

Page 346: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 346

<html>

<body>

<form action='/create' method='post'>

Name: <input name='name'>

<p>

Age: <input name='age'>

<p>

<input type=submit value='Submit form!'>

</form>

</body>

</html>

Page 347: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 347

<html>

<body>

<form action='/create' method='post'>

Name: <input name='name'>

<p>

Age: <input name='age'>

<p>

<input type=submit value='Submit form!'>

</form>

</body>

</html>

Page 348: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 348

<html>

<body>

<form action='/create' method='post'>

Name: <input name='name'>

<p>

Age: <input name='age'>

<p>

<input type=submit value='Submit form!'>

</form>

</body>

</html>

Page 349: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 349

Page 350: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 350

Page 351: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 351

Page 352: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 352

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 353: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 353

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 354: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 354

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 355: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 355

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 356: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 356

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 357: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 357

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 358: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 358

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 359: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 359

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 360: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 360

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 361: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 361

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 362: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 362

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 363: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 363

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 364: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 364

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 365: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 365

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 366: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 366

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 367: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 367

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 368: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 368

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 369: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 369

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 370: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 370

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 371: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 371

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 372: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 372

Page 373: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 373

/* This is index.js */

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Person = require('./Person.js');

. . .

// this is at the bottom

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

app.use('/', (req, res) =>

{ res.redirect('/public/personform.html'); } );

app.listen(3000, () =>

{ console.log('Listening on port 3000'); } );

Page 374: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 374

/* This is index.js */

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Person = require('./Person.js');

. . .

// this is at the bottom

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

app.use('/', (req, res) =>

{ res.redirect('/public/personform.html'); } );

app.listen(3000, () =>

{ console.log('Listening on port 3000'); } );

Page 375: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 375

/* This is index.js */

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Person = require('./Person.js');

. . .

// this is at the bottom

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

app.use('/', (req, res) =>

{ res.redirect('/public/personform.html'); } );

app.listen(3000, () =>

{ console.log('Listening on port 3000'); } );

Page 376: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 376

/* This is index.js */

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Person = require('./Person.js');

. . .

// this is at the bottom

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

app.use('/', (req, res) =>

{ res.redirect('/public/personform.html'); } );

app.listen(3000, () =>

{ console.log('Listening on port 3000'); } );

Page 377: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 377

/* This is index.js */

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Person = require('./Person.js');

. . .

// this is at the bottom

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

app.use('/', (req, res) =>

{ res.redirect('/public/personform.html'); } );

app.listen(3000, () =>

{ console.log('Listening on port 3000'); } );

Page 378: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 378

/* This is index.js */

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Person = require('./Person.js');

. . .

// this is at the bottom

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

app.use('/', (req, res) =>

{ res.redirect('/public/personform.html'); } );

app.listen(3000, () =>

{ console.log('Listening on port 3000'); } );

Page 379: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 379

/* This is index.js */

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Person = require('./Person.js');

. . .

// this is at the bottom

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

app.use('/', (req, res) =>

{ res.redirect('/public/personform.html'); } );

app.listen(3000, () =>

{ console.log('Listening on port 3000'); } );

Page 380: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 380

/* This is index.js */

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Person = require('./Person.js');

. . .

// this is at the bottom

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

app.use('/', (req, res) =>

{ res.redirect('/public/personform.html'); } );

app.listen(3000, () =>

{ console.log('Listening on port 3000'); } );

Page 381: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 381

Page 382: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 382

Page 383: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 383

app.use('/create', (req, res) => {

var newPerson = new Person ({ // defined in Person.js

name: req.body.name,

age: req.body.age,

});

newPerson.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { person: newPerson });

}

});

});

Page 384: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 384

app.use('/create', (req, res) => {

var newPerson = new Person ({ // defined in Person.js

name: req.body.name,

age: req.body.age,

});

newPerson.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { person: newPerson });

}

});

});

Page 385: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 385

app.use('/create', (req, res) => {

var newPerson = new Person ({ // defined in Person.js

name: req.body.name,

age: req.body.age,

});

newPerson.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { person: newPerson });

}

});

});

Page 386: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 386

app.use('/create', (req, res) => {

var newPerson = new Person ({ // defined in Person.js

name: req.body.name,

age: req.body.age,

});

newPerson.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { person: newPerson });

}

});

});

Page 387: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 387

app.use('/create', (req, res) => {

var newPerson = new Person ({ // defined in Person.js

name: req.body.name,

age: req.body.age,

});

newPerson.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { person: newPerson });

}

});

});

Page 388: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 388

app.use('/create', (req, res) => {

var newPerson = new Person ({ // defined in Person.js

name: req.body.name,

age: req.body.age,

});

newPerson.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { person: newPerson });

}

});

});

Page 389: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 389

app.use('/create', (req, res) => {

var newPerson = new Person ({ // defined in Person.js

name: req.body.name,

age: req.body.age,

});

newPerson.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { person: newPerson });

}

});

});

Page 390: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 390

<html>

<body>

<form action='/create' method='post'>

Name: <input name='name'>

<p>

Age: <input name='age'>

<p>

<input type=submit value='Submit form!'>

</form>

</body>

</html>

Page 391: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 391

app.use('/create', (req, res) => {

var newPerson = new Person ({ // defined in Person.js

name: req.body.name,

age: req.body.age,

});

newPerson.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { person: newPerson });

}

});

});

Page 392: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 392

app.use('/create', (req, res) => {

var newPerson = new Person ({ // defined in Person.js

name: req.body.name,

age: req.body.age,

});

newPerson.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { person: newPerson });

}

});

});

Page 393: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 393

app.use('/create', (req, res) => {

var newPerson = new Person ({ // defined in Person.js

name: req.body.name,

age: req.body.age,

});

newPerson.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { person: newPerson });

}

});

});

Page 394: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 394

app.use('/create', (req, res) => {

var newPerson = new Person ({ // defined in Person.js

name: req.body.name,

age: req.body.age,

});

newPerson.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { person: newPerson });

}

});

});

Page 395: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 395

app.use('/create', (req, res) => {

var newPerson = new Person ({ // defined in Person.js

name: req.body.name,

age: req.body.age,

});

newPerson.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { person: newPerson });

}

});

});

Page 396: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 396

app.use('/create', (req, res) => {

var newPerson = new Person ({ // defined in Person.js

name: req.body.name,

age: req.body.age,

});

newPerson.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { person: newPerson });

}

});

});

Page 397: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 397

app.use('/create', (req, res) => {

var newPerson = new Person ({ // defined in Person.js

name: req.body.name,

age: req.body.age,

});

newPerson.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { person: newPerson });

}

});

});

Page 398: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 398

app.use('/create', (req, res) => {

var newPerson = new Person ({ // defined in Person.js

name: req.body.name,

age: req.body.age,

});

newPerson.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { person: newPerson });

}

});

});

Page 399: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 399

app.use('/create', (req, res) => {

var newPerson = new Person ({ // defined in Person.js

name: req.body.name,

age: req.body.age,

});

newPerson.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { person: newPerson });

}

});

});

Page 400: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 400

app.use('/create', (req, res) => {

var newPerson = new Person ({ // defined in Person.js

name: req.body.name,

age: req.body.age,

});

newPerson.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { person: newPerson });

}

});

});

Page 401: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 401

<!-- This is views/created.ejs -->

Successfully created new person:

<p>

<b>Name:</b> <%= person.name %>

<br>

<b>Age:</b> <%= person.age %>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 402: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 402

<!-- This is views/created.ejs -->

Successfully created new person:

<p>

<b>Name:</b> <%= person.name %>

<br>

<b>Age:</b> <%= person.age %>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 403: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 403

<!-- This is views/created.ejs -->

Successfully created new person:

<p>

<b>Name:</b> <%= person.name %>

<br>

<b>Age:</b> <%= person.age %>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 404: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 404

<!-- This is views/created.ejs -->

Successfully created new person:

<p>

<b>Name:</b> <%= person.name %>

<br>

<b>Age:</b> <%= person.age %>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 405: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 405

Page 406: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 406

Page 407: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 407

Page 408: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 408

Page 409: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 409

Page 410: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 410

Page 411: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Summary

• MongoDB is a NoSQL Database that is designed for use with JavaScript apps

• MongoDB stores collections of documents

• We can access MongoDB from our Node/Express app using libraries such as Mongoose

• We define a Schema and then can create new documents using the save function

SD4x-4.6 411

Page 412: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Video 4.7

MongoDB: Select and Update

Chris Murphy

SD4x-4.7 412

Page 413: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Review

• MongoDB is a NoSQL Database that is designed for use with JavaScript apps

• MongoDB stores collections of documents

• We can access MongoDB from our Node/Express app using libraries such as Mongoose

• We define a Schema and then can create new documents using the save function

SD4x-4.7 413

Page 414: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 414

Client Server Database

Page 415: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 415

Page 416: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 416

Page 417: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 417

Page 418: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 418

Page 419: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 419

Page 420: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 420

Page 421: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 421

app.use('/all', (req, res) => {

Person.find( (err, allPeople) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (allPeople.length == 0) {

res.type('html').status(200);

res.send('There are no people');

}

else {

res.render('showAll', { people: allPeople });

}

});

});

Page 422: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 422

app.use('/all', (req, res) => {

Person.find( (err, allPeople) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (allPeople.length == 0) {

res.type('html').status(200);

res.send('There are no people');

}

else {

res.render('showAll', { people: allPeople });

}

});

});

Page 423: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 423

app.use('/all', (req, res) => {

Person.find( (err, allPeople) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (allPeople.length == 0) {

res.type('html').status(200);

res.send('There are no people');

}

else {

res.render('showAll', { people: allPeople });

}

});

});

Page 424: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 424

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 425: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 425

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 426: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.6 426

/* This is Person.js */

var mongoose = require('mongoose');

// note: your host/port number may be different!

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var personSchema = new Schema( {

name: {type: String, required: true, unique: true},

age: Number

} );

module.exports = mongoose.model('Person', personSchema);

Page 427: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 427

app.use('/all', (req, res) => {

Person.find( (err, allPeople) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (allPeople.length == 0) {

res.type('html').status(200);

res.send('There are no people');

}

else {

res.render('showAll', { people: allPeople });

}

});

});

Page 428: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 428

app.use('/all', (req, res) => {

Person.find( (err, allPeople) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (allPeople.length == 0) {

res.type('html').status(200);

res.send('There are no people');

}

else {

res.render('showAll', { people: allPeople });

}

});

});

Page 429: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 429

app.use('/all', (req, res) => {

Person.find( (err, allPeople) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (allPeople.length == 0) {

res.type('html').status(200);

res.send('There are no people');

}

else {

res.render('showAll', { people: allPeople });

}

});

});

Page 430: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 430

app.use('/all', (req, res) => {

Person.find( (err, allPeople) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (allPeople.length == 0) {

res.type('html').status(200);

res.send('There are no people');

}

else {

res.render('showAll', { people: allPeople });

}

});

});

Page 431: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 431

app.use('/all', (req, res) => {

Person.find( (err, allPeople) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (allPeople.length == 0) {

res.type('html').status(200);

res.send('There are no people');

}

else {

res.render('showAll', { people: allPeople });

}

});

});

Page 432: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 432

app.use('/all', (req, res) => {

Person.find( (err, allPeople) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (allPeople.length == 0) {

res.type('html').status(200);

res.send('There are no people');

}

else {

res.render('showAll', { people: allPeople });

}

});

});

Page 433: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 433

app.use('/all', (req, res) => {

Person.find( (err, allPeople) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (allPeople.length == 0) {

res.type('html').status(200);

res.send('There are no people');

}

else {

res.render('showAll', { people: allPeople });

}

});

});

Page 434: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 434

app.use('/all', (req, res) => {

Person.find( (err, allPeople) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (allPeople.length == 0) {

res.type('html').status(200);

res.send('There are no people');

}

else {

res.render('showAll', { people: allPeople });

}

});

});

Page 435: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 435

app.use('/all', (req, res) => {

Person.find( (err, allPeople) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (allPeople.length == 0) {

res.type('html').status(200);

res.send('There are no people');

}

else {

res.render('showAll', { people: allPeople });

}

});

});

Page 436: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 436

<!-- This is views/showAll.ejs -->

Here are all the people:

<ul>

<% people.forEach( (person) => { %>

<li><a href="/person?name=<%= person.name %>">

<%= person.name %></a>:

<%= person.age %>

</li>

<% }); %>

</ul>

<br><a href='/public/personform.html'>Create New Person</a>

Page 437: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 437

<!-- This is views/showAll.ejs -->

Here are all the people:

<ul>

<% people.forEach( (person) => { %>

<li><a href="/person?name=<%= person.name %>">

<%= person.name %></a>:

<%= person.age %>

</li>

<% }); %>

</ul>

<br><a href='/public/personform.html'>Create New Person</a>

Page 438: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 438

<!-- This is views/showAll.ejs -->

Here are all the people:

<ul>

<% people.forEach( (person) => { %>

<li><a href="/person?name=<%= person.name %>">

<%= person.name %></a>:

<%= person.age %>

</li>

<% }); %>

</ul>

<br><a href='/public/personform.html'>Create New Person</a>

Page 439: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 439

<!-- This is views/showAll.ejs -->

Here are all the people:

<ul>

<% people.forEach( (person) => { %>

<li><a href="/person?name=<%= person.name %>">

<%= person.name %></a>:

<%= person.age %>

</li>

<% }); %>

</ul>

<br><a href='/public/personform.html'>Create New Person</a>

Page 440: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 440

<!-- This is views/showAll.ejs -->

Here are all the people:

<ul>

<% people.forEach( (person) => { %>

<li><a href="/person?name=<%= person.name %>">

<%= person.name %></a>:

<%= person.age %>

</li>

<% }); %>

</ul>

<br><a href='/public/personform.html'>Create New Person</a>

Page 441: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 441

<!-- This is views/showAll.ejs -->

Here are all the people:

<ul>

<% people.forEach( (person) => { %>

<li><a href="/person?name=<%= person.name %>">

<%= person.name %></a>:

<%= person.age %>

</li>

<% }); %>

</ul>

<br><a href='/public/personform.html'>Create New Person</a>

Page 442: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 442

<!-- This is views/showAll.ejs -->

Here are all the people:

<ul>

<% people.forEach( (person) => { %>

<li><a href="/person?name=<%= person.name %>">

<%= person.name %></a>:

<%= person.age %>

</li>

<% }); %>

</ul>

<br><a href='/public/personform.html'>Create New Person</a>

Page 443: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 443

<!-- This is views/showAll.ejs -->

Here are all the people:

<ul>

<% people.forEach( (person) => { %>

<li><a href="/person?name=<%= person.name %>">

<%= person.name %></a>:

<%= person.age %>

</li>

<% }); %>

</ul>

<br><a href='/public/personform.html'>Create New Person</a>

Page 444: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 444

<!-- This is views/showAll.ejs -->

Here are all the people:

<ul>

<% people.forEach( (person) => { %>

<li><a href="/person?name=<%= person.name %>">

<%= person.name %></a>:

<%= person.age %>

</li>

<% }); %>

</ul>

<br><a href='/public/personform.html'>Create New Person</a>

Page 445: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 445

<!-- This is views/showAll.ejs -->

Here are all the people:

<ul>

<% people.forEach( (person) => { %>

<li><a href="/person?name=<%= person.name %>">

<%= person.name %></a>:

<%= person.age %>

</li>

<% }); %>

</ul>

<br><a href='/public/personform.html'>Create New Person</a>

Page 446: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 446

<!-- This is views/showAll.ejs -->

Here are all the people:

<ul>

<% people.forEach( (person) => { %>

<li><a href="/person?name=<%= person.name %>">

<%= person.name %></a>:

<%= person.age %>

</li>

<% }); %>

</ul>

<br><a href='/public/personform.html'>Create New Person</a>

Page 447: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 447

<!-- This is views/showAll.ejs -->

Here are all the people:

<ul>

<% people.forEach( (person) => { %>

<li><a href="/person?name=<%= person.name %>">

<%= person.name %></a>:

<%= person.age %>

</li>

<% }); %>

</ul>

<br><a href='/public/personform.html'>Create New Person</a>

Page 448: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 448

<!-- This is views/showAll.ejs -->

Here are all the people:

<ul>

<% people.forEach( (person) => { %>

<li><a href="/person?name=<%= person.name %>">

<%= person.name %></a>:

<%= person.age %>

</li>

<% }); %>

</ul>

<br><a href='/public/personform.html'>Create New Person</a>

Page 449: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 449

<!-- This is views/showAll.ejs -->

Here are all the people:

<ul>

<% people.forEach( (person) => { %>

<li><a href="/person?name=<%= person.name %>">

<%= person.name %></a>:

<%= person.age %>

</li>

<% }); %>

</ul>

<br><a href='/public/personform.html'>Create New Person</a>

Page 450: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 450

Page 451: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 451

Page 452: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 452

Page 453: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 453

Page 454: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 454

Page 455: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 455

app.use('/person', (req, res) => {

var searchName = req.query.name;

Person.findOne( { name: searchName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + searchName);

}

else {

res.render('personInfo', { person : person });

}

});

});

Page 456: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 456

app.use('/person', (req, res) => {

var searchName = req.query.name;

Person.findOne( { name: searchName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + searchName);

}

else {

res.render('personInfo', { person : person });

}

});

});

Page 457: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 457

app.use('/person', (req, res) => {

var searchName = req.query.name;

Person.findOne( { name: searchName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + searchName);

}

else {

res.render('personInfo', { person : person });

}

});

});

Page 458: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 458

app.use('/person', (req, res) => {

var searchName = req.query.name;

Person.findOne( { name: searchName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + searchName);

}

else {

res.render('personInfo', { person : person });

}

});

});

Page 459: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 459

app.use('/person', (req, res) => {

var searchName = req.query.name;

Person.findOne( { name: searchName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + searchName);

}

else {

res.render('personInfo', { person : person });

}

});

});

Page 460: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 460

app.use('/person', (req, res) => {

var searchName = req.query.name;

Person.findOne( { name: searchName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + searchName);

}

else {

res.render('personInfo', { person : person });

}

});

});

Page 461: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 461

app.use('/person', (req, res) => {

var searchName = req.query.name;

Person.findOne( { name: searchName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + searchName);

}

else {

res.render('personInfo', { person : person });

}

});

});

Page 462: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 462

app.use('/person', (req, res) => {

var searchName = req.query.name;

Person.findOne( { name: searchName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + searchName);

}

else {

res.render('personInfo', { person : person });

}

});

});

Page 463: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 463

app.use('/person', (req, res) => {

var searchName = req.query.name;

Person.findOne( { name: searchName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + searchName);

}

else {

res.render('personInfo', { person : person });

}

});

});

Page 464: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 464

app.use('/person', (req, res) => {

var searchName = req.query.name;

Person.findOne( { name: searchName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + searchName);

}

else {

res.render('personInfo', { person : person });

}

});

});

Page 465: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 465

app.use('/person', (req, res) => {

var searchName = req.query.name;

Person.findOne( { name: searchName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + searchName);

}

else {

res.render('personInfo', { person : person });

}

});

});

Page 466: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 466

app.use('/person', (req, res) => {

var searchName = req.query.name;

Person.findOne( { name: searchName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + searchName);

}

else {

res.render('personInfo', { person : person });

}

});

});

Page 467: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 467

app.use('/person', (req, res) => {

var searchName = req.query.name;

Person.findOne( { name: searchName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + searchName);

}

else {

res.render('personInfo', { person : person });

}

});

});

Page 468: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 468

app.use('/person', (req, res) => {

var searchName = req.query.name;

Person.findOne( { name: searchName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + searchName);

}

else {

res.render('personInfo', { person : person });

}

});

});

Page 469: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 469

app.use('/person', (req, res) => {

var searchName = req.query.name;

Person.findOne( { name: searchName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + searchName);

}

else {

res.render('personInfo', { person : person });

}

});

});

Page 470: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 470

app.use('/person', (req, res) => {

var searchName = req.query.name;

Person.findOne( { name: searchName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + searchName);

}

else {

res.render('personInfo', { person : person });

}

});

});

Page 471: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 471

<!-- This is views/personInfo.ejs -->

<form action='/update' method='post'>

Name: <%= person.name %><br>

<input name='username' value='<%= person.name %>' hidden>

Age: <input name='age' value='<%= person.age %>'>

<input type='submit' value='Update'>

</form>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 472: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 472

<!-- This is views/personInfo.ejs -->

<form action='/update' method='post'>

Name: <%= person.name %><br>

<input name='username' value='<%= person.name %>' hidden>

Age: <input name='age' value='<%= person.age %>'>

<input type='submit' value='Update'>

</form>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 473: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 473

<!-- This is views/personInfo.ejs -->

<form action='/update' method='post'>

Name: <%= person.name %><br>

<input name='username' value='<%= person.name %>' hidden>

Age: <input name='age' value='<%= person.age %>'>

<input type='submit' value='Update'>

</form>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 474: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 474

<!-- This is views/personInfo.ejs -->

<form action='/update' method='post'>

Name: <%= person.name %><br>

<input name='username' value='<%= person.name %>' hidden>

Age: <input name='age' value='<%= person.age %>'>

<input type='submit' value='Update'>

</form>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 475: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 475

<!-- This is views/personInfo.ejs -->

<form action='/update' method='post'>

Name: <%= person.name %><br>

<input name='username' value='<%= person.name %>' hidden>

Age: <input name='age' value='<%= person.age %>'>

<input type='submit' value='Update'>

</form>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 476: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 476

<!-- This is views/personInfo.ejs -->

<form action='/update' method='post'>

Name: <%= person.name %><br>

<input name='username' value='<%= person.name %>' hidden>

Age: <input name='age' value='<%= person.age %>'>

<input type='submit' value='Update'>

</form>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 477: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 477

<!-- This is views/personInfo.ejs -->

<form action='/update' method='post'>

Name: <%= person.name %><br>

<input name='username' value='<%= person.name %>' hidden>

Age: <input name='age' value='<%= person.age %>'>

<input type='submit' value='Update'>

</form>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 478: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 478

<!-- This is views/personInfo.ejs -->

<form action='/update' method='post'>

Name: <%= person.name %><br>

<input name='username' value='<%= person.name %>' hidden>

Age: <input name='age' value='<%= person.age %>'>

<input type='submit' value='Update'>

</form>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 479: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 479

<!-- This is views/personInfo.ejs -->

<form action='/update' method='post'>

Name: <%= person.name %><br>

<input name='username' value='<%= person.name %>' hidden>

Age: <input name='age' value='<%= person.age %>'>

<input type='submit' value='Update'>

</form>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 480: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 480

<!-- This is views/personInfo.ejs -->

<form action='/update' method='post'>

Name: <%= person.name %><br>

<input name='username' value='<%= person.name %>' hidden>

Age: <input name='age' value='<%= person.age %>'>

<input type='submit' value='Update'>

</form>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 481: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 481

<!-- This is views/personInfo.ejs -->

<form action='/update' method='post'>

Name: <%= person.name %><br>

<input name='username' value='<%= person.name %>' hidden>

Age: <input name='age' value='<%= person.age %>'>

<input type='submit' value='Update'>

</form>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 482: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 482

<!-- This is views/personInfo.ejs -->

<form action='/update' method='post'>

Name: <%= person.name %><br>

<input name='username' value='<%= person.name %>' hidden>

Age: <input name='age' value='<%= person.age %>'>

<input type='submit' value='Update'>

</form>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 483: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 483

Page 484: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 484

Page 485: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 485

Page 486: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 486

Page 487: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 487

Page 488: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 488

app.use('/update', (req, res) => {

var updateName = req.body.username;

Person.findOne( { name: updateName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + updateName);

}

else {

person.age = req.body.age;

person.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('updated', { person: person });

}

});

}

});

});

Page 489: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 489

app.use('/update', (req, res) => {

var updateName = req.body.username;

Person.findOne( { name: updateName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + updateName);

}

else {

person.age = req.body.age;

person.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('updated', { person: person });

}

});

}

});

});

Page 490: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 490

app.use('/update', (req, res) => {

var updateName = req.body.username;

Person.findOne( { name: updateName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + updateName);

}

else {

person.age = req.body.age;

person.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('updated', { person: person });

}

});

}

});

});

Page 491: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 491

app.use('/update', (req, res) => {

var updateName = req.body.username;

Person.findOne( { name: updateName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + updateName);

}

else {

person.age = req.body.age;

person.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('updated', { person: person });

}

});

}

});

});

Page 492: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 492

app.use('/update', (req, res) => {

var updateName = req.body.username;

Person.findOne( { name: updateName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + updateName);

}

else {

person.age = req.body.age;

person.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('updated', { person: person });

}

});

}

});

});

Page 493: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 493

app.use('/update', (req, res) => {

var updateName = req.body.username;

Person.findOne( { name: updateName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + updateName);

}

else {

person.age = req.body.age;

person.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('updated', { person: person });

}

});

}

});

});

Page 494: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 494

app.use('/update', (req, res) => {

var updateName = req.body.username;

Person.findOne( { name: updateName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + updateName);

}

else {

person.age = req.body.age;

person.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('updated', { person: person });

}

});

}

});

});

Page 495: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 495

app.use('/update', (req, res) => {

var updateName = req.body.username;

Person.findOne( { name: updateName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + updateName);

}

else {

person.age = req.body.age;

person.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('updated', { person: person });

}

});

}

});

});

Page 496: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 496

app.use('/update', (req, res) => {

var updateName = req.body.username;

Person.findOne( { name: updateName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + updateName);

}

else {

person.age = req.body.age;

person.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('updated', { person: person });

}

});

}

});

});

Page 497: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 497

app.use('/update', (req, res) => {

var updateName = req.body.username;

Person.findOne( { name: updateName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + updateName);

}

else {

person.age = req.body.age;

person.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('updated', { person: person });

}

});

}

});

});

Page 498: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 498

app.use('/update', (req, res) => {

var updateName = req.body.username;

Person.findOne( { name: updateName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + updateName);

}

else {

person.age = req.body.age;

person.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('updated', { person: person });

}

});

}

});

});

Page 499: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 499

app.use('/update', (req, res) => {

var updateName = req.body.username;

Person.findOne( { name: updateName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + updateName);

}

else {

person.age = req.body.age;

person.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('updated', { person: person });

}

});

}

});

});

Page 500: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 500

app.use('/update', (req, res) => {

var updateName = req.body.username;

Person.findOne( { name: updateName }, (err, person) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else if (!person) {

res.type('html').status(200);

res.send('No person named ' + updateName);

}

else {

person.age = req.body.age;

person.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('updated', { person: person });

}

});

}

});

});

Page 501: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 501

<!-- This is views/updated.ejs -->

Updated <%= person.name %>'s age to <%= person.age %>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 502: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 502

<!-- This is views/updated.ejs -->

Updated <%= person.name %>'s age to <%= person.age %>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 503: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 503

<!-- This is views/updated.ejs -->

Updated <%= person.name %>'s age to <%= person.age %>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 504: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.7 504

<!-- This is views/updated.ejs -->

Updated <%= person.name %>'s age to <%= person.age %>

<br><a href='/public/personform.html'>Create New Person</a>

<br><a href='/all'>Show All</a>

Page 505: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Summary

• We can access MongoDB from our Node/Express app using libraries such as Mongoose

• We can use the find function to select all documents in a collection, or pass a query object to select only certain ones

• Once we have a document, we can update it using the save function

SD4x-4.7 505

Page 506: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Video 4.8

MongoDB: Advanced Queries

Chris Murphy

SD4x-4.8 506

Page 507: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Review

• MongoDB is a NoSQL Database that stores collections of documents

• Once we’ve defined a Schema we can use the find function to select all documents in a collection, or pass a query object to select only certain ones

• Once we have a document, we can update it using the save function

SD4x-4.8 507

Page 508: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 508

{

title: 'Introduction to Algorithms',

year: 1990,

authors: [

{ name: 'Thomas Cormen', affiliation: 'Dartmouth' },

{ name: 'Charles Leiserson', affiliation: 'MIT' },

{ name: 'Ronald Rivest', affiliation: 'MIT' },

{ name: 'Clifford Stein', affiliation: 'Columbia' }

]

}

{

title: 'Principles of Compiler Design',

year: 1977,

authors: [

{ name: 'Alfred Aho', affiliation: 'Bell Labs' },

{ name: 'Jeffrey Ullman', affiliation: 'Princeton' }

]

}

Page 509: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 509

{

title: 'Introduction to Algorithms',

year: 1990,

authors: [

{ name: 'Thomas Cormen', affiliation: 'Dartmouth' },

{ name: 'Charles Leiserson', affiliation: 'MIT' },

{ name: 'Ronald Rivest', affiliation: 'MIT' },

{ name: 'Clifford Stein', affiliation: 'Columbia' }

]

}

{

title: 'Principles of Compiler Design',

year: 1977,

authors: [

{ name: 'Alfred Aho', affiliation: 'Bell Labs' },

{ name: 'Jeffrey Ullman', affiliation: 'Princeton' }

]

}

Page 510: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 510

{

title: 'Introduction to Algorithms',

year: 1990,

authors: [

{ name: 'Thomas Cormen', affiliation: 'Dartmouth' },

{ name: 'Charles Leiserson', affiliation: 'MIT' },

{ name: 'Ronald Rivest', affiliation: 'MIT' },

{ name: 'Clifford Stein', affiliation: 'Columbia' }

]

}

{

title: 'Principles of Compiler Design',

year: 1977,

authors: [

{ name: 'Alfred Aho', affiliation: 'Bell Labs' },

{ name: 'Jeffrey Ullman', affiliation: 'Princeton' }

]

}

Page 511: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 511

{

title: 'Introduction to Algorithms',

year: 1990,

authors: [

{ name: 'Thomas Cormen', affiliation: 'Dartmouth' },

{ name: 'Charles Leiserson', affiliation: 'MIT' },

{ name: 'Ronald Rivest', affiliation: 'MIT' },

{ name: 'Clifford Stein', affiliation: 'Columbia' }

]

}

{

title: 'Principles of Compiler Design',

year: 1977,

authors: [

{ name: 'Alfred Aho', affiliation: 'Bell Labs' },

{ name: 'Jeffrey Ullman', affiliation: 'Princeton' }

]

}

Page 512: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 512

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

module.exports = mongoose.model('Book', bookSchema);

Page 513: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 513

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

module.exports = mongoose.model('Book', bookSchema);

Page 514: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 514

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

module.exports = mongoose.model('Book', bookSchema);

Page 515: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 515

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

module.exports = mongoose.model('Book', bookSchema);

Page 516: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 516

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

module.exports = mongoose.model('Book', bookSchema);

Page 517: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 517

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

module.exports = mongoose.model('Book', bookSchema);

Page 518: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 518

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

module.exports = mongoose.model('Book', bookSchema);

Page 519: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 519

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

module.exports = mongoose.model('Book', bookSchema);

Page 520: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 520

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

module.exports = mongoose.model('Book', bookSchema);

Page 521: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 521

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

module.exports = mongoose.model('Book', bookSchema);

Page 522: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 522

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

module.exports = mongoose.model('Book', bookSchema);

Page 523: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 523

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

module.exports = mongoose.model('Book', bookSchema);

Page 524: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 524

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

module.exports = mongoose.model('Book', bookSchema);

Page 525: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 525

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

module.exports = mongoose.model('Book', bookSchema);

Page 526: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 526

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Schema = mongoose.Schema;

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

module.exports = mongoose.model('Book', bookSchema);

Page 527: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 527

Page 528: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 528

<form action='/createbook' method='post'>

Title: <input name='title'>

<p>

Year: <input name='year'>

<p>

Author #1: <br>

Name: <input name='authors[0][name]'> <br>

Affiliation: <input name='authors[0][affiliation]'>

<p>

Author #2: <br>

Name: <input name='authors[1][name]'> <br>

Affiliation: <input name='authors[1][affiliation]'>

<p>

<input type='submit' value='Submit'>

</form>

Page 529: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 529

<form action='/createbook' method='post'>

Title: <input name='title'>

<p>

Year: <input name='year'>

<p>

Author #1: <br>

Name: <input name='authors[0][name]'> <br>

Affiliation: <input name='authors[0][affiliation]'>

<p>

Author #2: <br>

Name: <input name='authors[1][name]'> <br>

Affiliation: <input name='authors[1][affiliation]'>

<p>

<input type='submit' value='Submit'>

</form>

Page 530: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 530

<form action='/createbook' method='post'>

Title: <input name='title'>

<p>

Year: <input name='year'>

<p>

Author #1: <br>

Name: <input name='authors[0][name]'> <br>

Affiliation: <input name='authors[0][affiliation]'>

<p>

Author #2: <br>

Name: <input name='authors[1][name]'> <br>

Affiliation: <input name='authors[1][affiliation]'>

<p>

<input type='submit' value='Submit'>

</form>

Page 531: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 531

<form action='/createbook' method='post'>

Title: <input name='title'>

<p>

Year: <input name='year'>

<p>

Author #1: <br>

Name: <input name='authors[0][name]'> <br>

Affiliation: <input name='authors[0][affiliation]'>

<p>

Author #2: <br>

Name: <input name='authors[1][name]'> <br>

Affiliation: <input name='authors[1][affiliation]'>

<p>

<input type='submit' value='Submit'>

</form>

Page 532: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 532

<form action='/createbook' method='post'>

Title: <input name='title'>

<p>

Year: <input name='year'>

<p>

Author #1: <br>

Name: <input name='authors[0][name]'> <br>

Affiliation: <input name='authors[0][affiliation]'>

<p>

Author #2: <br>

Name: <input name='authors[1][name]'> <br>

Affiliation: <input name='authors[1][affiliation]'>

<p>

<input type='submit' value='Submit'>

</form>

Page 533: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 533

<form action='/createbook' method='post'>

Title: <input name='title'>

<p>

Year: <input name='year'>

<p>

Author #1: <br>

Name: <input name='authors[0][name]'> <br>

Affiliation: <input name='authors[0][affiliation]'>

<p>

Author #2: <br>

Name: <input name='authors[1][name]'> <br>

Affiliation: <input name='authors[1][affiliation]'>

<p>

<input type='submit' value='Submit'>

</form>

Page 534: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 534

<form action='/createbook' method='post'>

Title: <input name='title'>

<p>

Year: <input name='year'>

<p>

Author #1: <br>

Name: <input name='authors[0][name]'> <br>

Affiliation: <input name='authors[0][affiliation]'>

<p>

Author #2: <br>

Name: <input name='authors[1][name]'> <br>

Affiliation: <input name='authors[1][affiliation]'>

<p>

<input type='submit' value='Submit'>

</form>

Page 535: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 535

<form action='/createbook' method='post'>

Title: <input name='title'>

<p>

Year: <input name='year'>

<p>

Author #1: <br>

Name: <input name='authors[0][name]'> <br>

Affiliation: <input name='authors[0][affiliation]'>

<p>

Author #2: <br>

Name: <input name='authors[1][name]'> <br>

Affiliation: <input name='authors[1][affiliation]'>

<p>

<input type='submit' value='Submit'>

</form>

Page 536: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 536

<form action='/createbook' method='post'>

Title: <input name='title'>

<p>

Year: <input name='year'>

<p>

Author #1: <br>

Name: <input name='authors[0][name]'> <br>

Affiliation: <input name='authors[0][affiliation]'>

<p>

Author #2: <br>

Name: <input name='authors[1][name]'> <br>

Affiliation: <input name='authors[1][affiliation]'>

<p>

<input type='submit' value='Submit'>

</form>

Page 537: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 537

Page 538: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 538

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Book = require('./Book.js');

app.use('/createbook', (req, res) => {

console.log(req.body);

var newBook = new Book(req.body);

newBook.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { book: newBook });

}

} );

});

Page 539: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 539

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Book = require('./Book.js');

app.use('/createbook', (req, res) => {

console.log(req.body);

var newBook = new Book(req.body);

newBook.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { book: newBook });

}

} );

});

Page 540: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 540

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Book = require('./Book.js');

app.use('/createbook', (req, res) => {

console.log(req.body);

var newBook = new Book(req.body);

newBook.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { book: newBook });

}

} );

});

Page 541: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 541

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Book = require('./Book.js');

app.use('/createbook', (req, res) => {

console.log(req.body);

var newBook = new Book(req.body);

newBook.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { book: newBook });

}

} );

});

Page 542: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 542

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Book = require('./Book.js');

app.use('/createbook', (req, res) => {

console.log(req.body);

var newBook = new Book(req.body);

newBook.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { book: newBook });

}

} );

});

Page 543: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 543

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Book = require('./Book.js');

app.use('/createbook', (req, res) => {

console.log(req.body);

var newBook = new Book(req.body);

newBook.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { book: newBook });

}

} );

});

Page 544: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 544

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Book = require('./Book.js');

app.use('/createbook', (req, res) => {

console.log(req.body);

var newBook = new Book(req.body);

newBook.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { book: newBook });

}

} );

});

Page 545: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 545

{ title: 'JavaScript Programming',

year: '2017',

authors:

[ { name: 'Chris Murphy', affiliation: 'UPenn' },

{ name: 'Swapneel Sheth', affiliation: 'UPenn' } ] }

Page 546: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 546

{ title: 'JavaScript Programming',

year: '2017',

authors:

[ { name: 'Chris Murphy', affiliation: 'UPenn' },

{ name: 'Swapneel Sheth', affiliation: 'UPenn' } ] }

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

Page 547: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 547

{ title: 'JavaScript Programming',

year: '2017',

authors:

[ { name: 'Chris Murphy', affiliation: 'UPenn' },

{ name: 'Swapneel Sheth', affiliation: 'UPenn' } ] }

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

Page 548: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 548

{ title: 'JavaScript Programming',

year: '2017',

authors:

[ { name: 'Chris Murphy', affiliation: 'UPenn' },

{ name: 'Swapneel Sheth', affiliation: 'UPenn' } ] }

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

Page 549: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 549

{ title: 'JavaScript Programming',

year: '2017',

authors:

[ { name: 'Chris Murphy', affiliation: 'UPenn' },

{ name: 'Swapneel Sheth', affiliation: 'UPenn' } ] }

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

Page 550: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 550

{ title: 'JavaScript Programming',

year: '2017',

authors:

[ { name: 'Chris Murphy', affiliation: 'UPenn' },

{ name: 'Swapneel Sheth', affiliation: 'UPenn' } ] }

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

Page 551: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 551

{ title: 'JavaScript Programming',

year: '2017',

authors:

[ { name: 'Chris Murphy', affiliation: 'UPenn' },

{ name: 'Swapneel Sheth', affiliation: 'UPenn' } ] }

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

Page 552: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 552

{ title: 'JavaScript Programming',

year: '2017',

authors:

[ { name: 'Chris Murphy', affiliation: 'UPenn' },

{ name: 'Swapneel Sheth', affiliation: 'UPenn' } ] }

var authorSchema = new Schema({

name: String,

affiliation: String

});

var bookSchema = new Schema({

title: {type: String, required: true, unique: true},

year: Number,

authors: [authorSchema]

});

Page 553: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 553

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Book = require('./Book.js');

app.use('/createbook', (req, res) => {

console.log(req.body);

var newBook = new Book(req.body);

newBook.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { book: newBook });

}

} );

});

Page 554: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 554

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Book = require('./Book.js');

app.use('/createbook', (req, res) => {

console.log(req.body);

var newBook = new Book(req.body);

newBook.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { book: newBook });

}

} );

});

Page 555: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 555

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Book = require('./Book.js');

app.use('/createbook', (req, res) => {

console.log(req.body);

var newBook = new Book(req.body);

newBook.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { book: newBook });

}

} );

});

Page 556: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 556

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Book = require('./Book.js');

app.use('/createbook', (req, res) => {

console.log(req.body);

var newBook = new Book(req.body);

newBook.save( (err) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('created', { book: newBook });

}

} );

});

Page 557: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 557

Page 558: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 558

Indicate the search criteria:

<form action='/search' method='post'>

Title: <input name='title'>

<p>

Author name: <input name='name'>

<p>

Year: <input name='year'>

<p>

<input type='radio' name='which' value='all' checked>All

<br>

<input type='radio' name='which' value='any'>Any

<p>

<input type='submit' value='Search'>

</form>

Page 559: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 559

Indicate the search criteria:

<form action='/search' method='post'>

Title: <input name='title'>

<p>

Author name: <input name='name'>

<p>

Year: <input name='year'>

<p>

<input type='radio' name='which' value='all' checked>All

<br>

<input type='radio' name='which' value='any'>Any

<p>

<input type='submit' value='Search'>

</form>

Page 560: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 560

Indicate the search criteria:

<form action='/search' method='post'>

Title: <input name='title'>

<p>

Author name: <input name='name'>

<p>

Year: <input name='year'>

<p>

<input type='radio' name='which' value='all' checked>All

<br>

<input type='radio' name='which' value='any'>Any

<p>

<input type='submit' value='Search'>

</form>

Page 561: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 561

Indicate the search criteria:

<form action='/search' method='post'>

Title: <input name='title'>

<p>

Author name: <input name='name'>

<p>

Year: <input name='year'>

<p>

<input type='radio' name='which' value='all' checked>All

<br>

<input type='radio' name='which' value='any'>Any

<p>

<input type='submit' value='Search'>

</form>

Page 562: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 562

Indicate the search criteria:

<form action='/search' method='post'>

Title: <input name='title'>

<p>

Author name: <input name='name'>

<p>

Year: <input name='year'>

<p>

<input type='radio' name='which' value='all' checked>All

<br>

<input type='radio' name='which' value='any'>Any

<p>

<input type='submit' value='Search'>

</form>

Page 563: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 563

Indicate the search criteria:

<form action='/search' method='post'>

Title: <input name='title'>

<p>

Author name: <input name='name'>

<p>

Year: <input name='year'>

<p>

<input type='radio' name='which' value='all' checked>All

<br>

<input type='radio' name='which' value='any'>Any

<p>

<input type='submit' value='Search'>

</form>

Page 564: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 564

Page 565: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 565

app.use('/search', (req, res) => {

if (req.body.which == 'all') {

searchAll(req, res);

}

else if (req.body.which == 'any') {

searchAny(req, res);

}

else {

searchAll(req, res);

}

});

Page 566: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 566

app.use('/search', (req, res) => {

if (req.body.which == 'all') {

searchAll(req, res);

}

else if (req.body.which == 'any') {

searchAny(req, res);

}

else {

searchAll(req, res);

}

});

Page 567: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 567

app.use('/search', (req, res) => {

if (req.body.which == 'all') {

searchAll(req, res);

}

else if (req.body.which == 'any') {

searchAny(req, res);

}

else {

searchAll(req, res);

}

});

Page 568: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 568

function searchAll(req, res) {

var query = {};

if (req.body.title) query.title = req.body.title;

if (req.body.year) query.year = req.body.year;

if (req.body.name) {

query['authors.name'] = req.body.name;

}

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 569: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 569

function searchAll(req, res) {

var query = {};

if (req.body.title) query.title = req.body.title;

if (req.body.year) query.year = req.body.year;

if (req.body.name) {

query['authors.name'] = req.body.name;

}

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 570: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 570

function searchAll(req, res) {

var query = {};

if (req.body.title) query.title = req.body.title;

if (req.body.year) query.year = req.body.year;

if (req.body.name) {

query['authors.name'] = req.body.name;

}

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 571: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 571

function searchAll(req, res) {

var query = {};

if (req.body.title) query.title = req.body.title;

if (req.body.year) query.year = req.body.year;

if (req.body.name) {

query['authors.name'] = req.body.name;

}

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 572: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 572

function searchAll(req, res) {

var query = {};

if (req.body.title) query.title = req.body.title;

if (req.body.year) query.year = req.body.year;

if (req.body.name) {

query['authors.name'] = req.body.name;

}

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 573: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 573

function searchAll(req, res) {

var query = {};

if (req.body.title) query.title = req.body.title;

if (req.body.year) query.year = req.body.year;

if (req.body.name) {

query['authors.name'] = req.body.name;

}

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 574: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 574

function searchAll(req, res) {

var query = {};

if (req.body.title) query.title = req.body.title;

if (req.body.year) query.year = req.body.year;

if (req.body.name) {

query['authors.name'] = req.body.name;

}

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 575: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 575

function searchAll(req, res) {

var query = {};

if (req.body.title) query.title = req.body.title;

if (req.body.year) query.year = req.body.year;

if (req.body.name) {

query['authors.name'] = req.body.name;

}

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 576: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 576

function searchAll(req, res) {

var query = {};

if (req.body.title) query.title = req.body.title;

if (req.body.year) query.year = req.body.year;

if (req.body.name) {

query['authors.name'] = req.body.name;

}

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 577: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 577

function searchAll(req, res) {

var query = {};

if (req.body.title) query.title = req.body.title;

if (req.body.year) query.year = req.body.year;

if (req.body.name) {

query['authors.name'] = req.body.name;

}

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 578: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 578

function searchAll(req, res) {

var query = {};

if (req.body.title) query.title = req.body.title;

if (req.body.year) query.year = req.body.year;

if (req.body.name) {

query['authors.name'] = req.body.name;

}

console.log(query);

// { 'authors.name': 'Chris Murphy', year: '2017' }

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 579: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 579

function searchAll(req, res) {

var query = {};

if (req.body.title) query.title = req.body.title;

if (req.body.year) query.year = req.body.year;

if (req.body.name) {

query['authors.name'] = req.body.name;

}

console.log(query);

// { 'authors.name': 'Chris Murphy', year: '2017' }

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 580: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 580

function searchAll(req, res) {

var query = {};

if (req.body.title) query.title = req.body.title;

if (req.body.year) query.year = req.body.year;

if (req.body.name) {

query['authors.name'] = req.body.name;

}

console.log(query);

// { 'authors.name': 'Chris Murphy', year: '2017' }

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 581: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 581

function searchAll(req, res) {

var query = {};

if (req.body.title) query.title = req.body.title;

if (req.body.year) query.year = req.body.year;

if (req.body.name) {

query['authors.name'] = req.body.name;

}

console.log(query);

// { 'authors.name': 'Chris Murphy', year: '2017' }

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 582: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 582

Page 583: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 583

<!-- This is views/books.ejs -->

Here are the results of your search:

<ul>

<% books.forEach( (book) => { %>

<li>

<i><%= book.title %></i>.

<% book.authors.forEach( (author) => { %>

<%= author.name %>,

<% }); %>

<%= book.year %>.

</li>

<% }); %>

</ul>

Page 584: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 584

<!-- This is views/books.ejs -->

Here are the results of your search:

<ul>

<% books.forEach( (book) => { %>

<li>

<i><%= book.title %></i>.

<% book.authors.forEach( (author) => { %>

<%= author.name %>,

<% }); %>

<%= book.year %>.

</li>

<% }); %>

</ul>

Page 585: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 585

<!-- This is views/books.ejs -->

Here are the results of your search:

<ul>

<% books.forEach( (book) => { %>

<li>

<i><%= book.title %></i>.

<% book.authors.forEach( (author) => { %>

<%= author.name %>,

<% }); %>

<%= book.year %>.

</li>

<% }); %>

</ul>

Page 586: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 586

<!-- This is views/books.ejs -->

Here are the results of your search:

<ul>

<% books.forEach( (book) => { %>

<li>

<i><%= book.title %></i>.

<% book.authors.forEach( (author) => { %>

<%= author.name %>,

<% }); %>

<%= book.year %>.

</li>

<% }); %>

</ul>

Page 587: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 587

<!-- This is views/books.ejs -->

Here are the results of your search:

<ul>

<% books.forEach( (book) => { %>

<li>

<i><%= book.title %></i>.

<% book.authors.forEach( (author) => { %>

<%= author.name %>,

<% }); %>

<%= book.year %>.

</li>

<% }); %>

</ul>

Page 588: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 588

<!-- This is views/books.ejs -->

Here are the results of your search:

<ul>

<% books.forEach( (book) => { %>

<li>

<i><%= book.title %></i>.

<% book.authors.forEach( (author) => { %>

<%= author.name %>,

<% }); %>

<%= book.year %>.

</li>

<% }); %>

</ul>

Page 589: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 589

<!-- This is views/books.ejs -->

Here are the results of your search:

<ul>

<% books.forEach( (book) => { %>

<li>

<i><%= book.title %></i>.

<% book.authors.forEach( (author) => { %>

<%= author.name %>,

<% }); %>

<%= book.year %>.

</li>

<% }); %>

</ul>

Page 590: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 590

<!-- This is views/books.ejs -->

Here are the results of your search:

<ul>

<% books.forEach( (book) => { %>

<li>

<i><%= book.title %></i>.

<% book.authors.forEach( (author) => { %>

<%= author.name %>,

<% }); %>

<%= book.year %>.

</li>

<% }); %>

</ul>

Page 591: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 591

<!-- This is views/books.ejs -->

Here are the results of your search:

<ul>

<% books.forEach( (book) => { %>

<li>

<i><%= book.title %></i>.

<% book.authors.forEach( (author) => { %>

<%= author.name %>,

<% }); %>

<%= book.year %>.

</li>

<% }); %>

</ul>

Page 592: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 592

Page 593: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 593

app.use('/search', (req, res) => {

if (req.body.which == 'all') {

searchAll(req, res);

}

else if (req.body.which == 'any') {

searchAny(req, res);

}

else {

searchAll(req, res);

}

});

Page 594: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 594

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year)

terms.push( { year: req.body.year });

if (req.body.name)

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 595: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 595

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year)

terms.push( { year: req.body.year });

if (req.body.name)

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 596: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 596

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year)

terms.push( { year: req.body.year });

if (req.body.name)

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 597: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 597

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year)

terms.push( { year: req.body.year });

if (req.body.name)

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 598: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 598

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year)

terms.push( { year: req.body.year });

if (req.body.name)

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 599: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 599

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year)

terms.push( { year: req.body.year });

if (req.body.name)

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 600: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 600

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year)

terms.push( { year: req.body.year });

if (req.body.name)

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 601: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 601

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year)

terms.push( { year: req.body.year });

if (req.body.name)

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 602: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 602

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year)

terms.push( { year: req.body.year });

if (req.body.name)

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 603: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 603

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year)

terms.push( { year: req.body.year });

if (req.body.name)

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 604: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 604

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year)

terms.push( { year: req.body.year });

if (req.body.name)

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 605: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 605

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year)

terms.push( { year: req.body.year });

if (req.body.name)

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 606: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 606

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year)

terms.push( { year: req.body.year });

if (req.body.name)

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 607: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 607

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year)

terms.push( { year: req.body.year });

if (req.body.name)

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 608: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 608

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year)

terms.push( { year: req.body.year });

if (req.body.name)

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

// { '$or': [ {title: 'Programming'}, {year: '2017'} ] }

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 609: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 609

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year)

terms.push( { year: req.body.year });

if (req.body.name)

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 610: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 610

Page 611: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 611

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year) {

terms.push( { year: req.body.year });

if (req.body.name) {

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

// { '$or': [ {title: 'Programming'}, {year: '2017'} ] }

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

Page 612: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 612

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year) {

terms.push( { year: req.body.year });

if (req.body.name) {

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

// { '$or': [ {title: 'Programming'}, {year: '2017'} ] }

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

Page 613: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 613

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: req.body.title });

if (req.body.year) {

terms.push( { year: req.body.year });

if (req.body.name) {

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

// { '$or': [ {title: 'Programming'}, {year: '2017'} ] }

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

Page 614: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 614

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: { $regex: req.body.title } });

if (req.body.year) {

terms.push( { year: req.body.year });

if (req.body.name) {

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

// { '$or': [ {title: 'Programming'}, {year: '2017'} ] }

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

Page 615: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 615

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: { $regex: req.body.title } });

if (req.body.year) {

terms.push( { year: req.body.year });

if (req.body.name) {

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

// { '$or': [ {title: [Object]}, {year: '2017'} ] }

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

Page 616: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 616

Page 617: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 617

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: { $regex: req.body.title } });

if (req.body.year) {

terms.push( { year: req.body.year });

if (req.body.name) {

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

// { '$or': [ {title: [Object]}, {year: '2017'} ] }

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

Page 618: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 618

function searchAny(req, res) {

var terms = [];

if (req.body.title)

terms.push( { title: { $regex: req.body.title } });

if (req.body.year) {

terms.push( { year: req.body.year });

if (req.body.name) {

terms.push( { 'authors.name' : req.body.name });

var query = { $or : terms };

console.log(query);

// { '$or': [ {title: [Object]}, {year: '2017'} ] }

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

}).sort( { 'title' : 'asc' } );

Page 619: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.8 619

Page 620: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Summary

• MongoDB allows us to have a Schema in which one document contains other documents

• We can then do queries for documents using the properties of the documents they contain

• We can also do “all” and “any” queries by passing objects to the find function

• And sort the results using sort

SD4x-4.8 620

Page 621: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Video 4.9

Creating an API

Chris Murphy

SD4x-4.9 621

Page 622: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Review

• Node.js and Express allow us to build server-side web apps in JavaScript

• MongoDB allows us to store data as documents and query them via JavaScript

SD4x-4.9 622

Page 623: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 623

Page 624: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 624

function searchAll(req, res) {

var query = {};

if (req.body.title) query.title = req.body.title;

if (req.body.year) query.year = req.body.year;

if (req.body.name) {

query['authors.name'] = req.body.name;

}

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 625: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 625

function searchAll(req, res) {

var query = {};

if (req.body.title) query.title = req.body.title;

if (req.body.year) query.year = req.body.year;

if (req.body.name) {

query['authors.name'] = req.body.name;

}

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 626: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 626

function searchAll(req, res) {

var query = {};

if (req.body.title) query.title = req.body.title;

if (req.body.year) query.year = req.body.year;

if (req.body.name) {

query['authors.name'] = req.body.name;

}

console.log(query);

Book.find( query, (err, books) => {

if (err) {

res.type('html').status(500);

res.send('Error: ' + err);

}

else {

res.render('books', { books: books });

}

});

}

Page 627: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 627

Page 628: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 628

Page 629: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 629

Page 630: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 630

Client Server Database

HTTP Request

HTTP Response

Entire HTML Page

Query

Results

Page 631: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 631

Client Server Database

HTTP Request

HTTP Response

Entire HTML Page

Query

Results

Page 632: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 632

Client Server Database

HTTP Request

HTTP Response

Just the Data!

Query

Results

Page 633: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

What is an API?

• An API is an Application Programming Interface

• It is a URL or a set of URLs that returns pure data to requests

• APIs can be used to incorporate data and functionality from other sources in your webapp

SD4x-4.9 633

Page 634: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

What is an API?

• An API is an Application Programming Interface

• It is a URL or a set of URLs that returns pure data to requests

• APIs can be used to incorporate data and functionality from other sources in your webapp

• You can also create your own API using Node.js to return JSON data to HTTP requests

SD4x-4.9 634

Page 635: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 635

GET /public/books.html

HTML

Page 636: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 636

Query

JSON

GET /api?...

GET /public/books.html

HTML

Result

Page 637: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 637

Result

Query

Query

JSON

GET /api?...

JSON

GET /api?...

GET /public/books.html

HTML

Result

Page 638: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 638

JSON

Result

Result

Query

Query

QueryGET /api?...

JSON

GET /api?...

JSON

GET /api?...

GET /public/books.html

HTML

Result

Page 639: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 639

Page 640: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 640

GET /public/books.html

HTML

Page 641: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 641

Page 642: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 642

Page 643: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 643

Query

JSON

GET /api?...

GET /public/books.html

HTML

Result

Page 644: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 644

Page 645: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 645

Page 646: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 646

Result

Query

Query

JSON

GET /api?...

JSON

GET /api?...

GET /public/books.html

HTML

Result

Page 647: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 647

Page 648: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 648

Page 649: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 649

Page 650: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 650

Page 651: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 651

Page 652: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 652

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Book = require('./Book.js');

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

. . .

Page 653: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 653

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Book = require('./Book.js');

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

. . .

Page 654: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 654

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Book = require('./Book.js');

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

. . .

Page 655: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 655

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Book = require('./Book.js');

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

. . .

Page 656: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 656

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Book = require('./Book.js');

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

. . .

Page 657: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 657

var express = require('express');

var app = express();

app.set('view engine', 'ejs');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

var Book = require('./Book.js');

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

. . .

Page 658: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 658

app.use( '/api', (req, res) => {

var query = {};

if (req.query.title)

query.title = { $regex : req.query.title };

if (req.query.name)

query['authors.name'] = { $regex: req.query.name };

if (req.query.year)

query.year = req.query.year;

if (Object.keys(query).length != 0) {

Book.find( query, (err, books) => {

if (!err)

res.json(books);

else {

console.log(err)

res.json({});

}

});

}

else res.json({}); // empty query

});

Page 659: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 659

app.use( '/api', (req, res) => {

var query = {};

if (req.query.title)

query.title = { $regex : req.query.title };

if (req.query.name)

query['authors.name'] = { $regex: req.query.name };

if (req.query.year)

query.year = req.query.year;

if (Object.keys(query).length != 0) {

Book.find( query, (err, books) => {

if (!err)

res.json(books);

else {

console.log(err)

res.json({});

}

});

}

else res.json({}); // empty query

});

Page 660: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 660

app.use( '/api', (req, res) => {

var query = {};

if (req.query.title)

query.title = { $regex : req.query.title };

if (req.query.name)

query['authors.name'] = { $regex: req.query.name };

if (req.query.year)

query.year = req.query.year;

if (Object.keys(query).length != 0) {

Book.find( query, (err, books) => {

if (!err)

res.json(books);

else {

console.log(err)

res.json({});

}

});

}

else res.json({}); // empty query

});

Page 661: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 661

app.use( '/api', (req, res) => {

var query = {};

if (req.query.title)

query.title = { $regex : req.query.title };

if (req.query.name)

query['authors.name'] = { $regex: req.query.name };

if (req.query.year)

query.year = req.query.year;

if (Object.keys(query).length != 0) {

Book.find( query, (err, books) => {

if (!err)

res.json(books);

else {

console.log(err)

res.json({});

}

});

}

else res.json({}); // empty query

});

Page 662: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 662

app.use( '/api', (req, res) => {

var query = {};

if (req.query.title)

query.title = { $regex : req.query.title };

if (req.query.name)

query['authors.name'] = { $regex: req.query.name };

if (req.query.year)

query.year = req.query.year;

if (Object.keys(query).length != 0) {

Book.find( query, (err, books) => {

if (!err)

res.json(books);

else {

console.log(err)

res.json({});

}

});

}

else res.json({}); // empty query

});

Page 663: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 663

app.use( '/api', (req, res) => {

var query = {};

if (req.query.title)

query.title = { $regex : req.query.title };

if (req.query.name)

query['authors.name'] = { $regex: req.query.name };

if (req.query.year)

query.year = req.query.year;

if (Object.keys(query).length != 0) {

Book.find( query, (err, books) => {

if (!err)

res.json(books);

else {

console.log(err)

res.json({});

}

});

}

else res.json({}); // empty query

});

Page 664: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 664

app.use( '/api', (req, res) => {

var query = {};

if (req.query.title)

query.title = { $regex : req.query.title };

if (req.query.name)

query['authors.name'] = { $regex: req.query.name };

if (req.query.year)

query.year = req.query.year;

if (Object.keys(query).length != 0) {

Book.find( query, (err, books) => {

if (!err)

res.json(books);

else {

console.log(err)

res.json({});

}

});

}

else res.json({}); // empty query

});

Page 665: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 665

app.use( '/api', (req, res) => {

var query = {};

if (req.query.title)

query.title = { $regex : req.query.title };

if (req.query.name)

query['authors.name'] = { $regex: req.query.name };

if (req.query.year)

query.year = req.query.year;

if (Object.keys(query).length != 0) {

Book.find( query, (err, books) => {

if (!err)

res.json(books);

else {

console.log(err)

res.json({});

}

});

}

else res.json({}); // empty query

});

Page 666: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 666

app.use( '/api', (req, res) => {

var query = {};

if (req.query.title)

query.title = { $regex : req.query.title };

if (req.query.name)

query['authors.name'] = { $regex: req.query.name };

if (req.query.year)

query.year = req.query.year;

if (Object.keys(query).length != 0) {

Book.find( query, (err, books) => {

if (!err)

res.json(books);

else {

console.log(err)

res.json({});

}

});

}

else res.json({}); // empty query

});

Page 667: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 667

app.use( '/api', (req, res) => {

var query = {};

if (req.query.title)

query.title = { $regex : req.query.title };

if (req.query.name)

query['authors.name'] = { $regex: req.query.name };

if (req.query.year)

query.year = req.query.year;

if (Object.keys(query).length != 0) {

Book.find( query, (err, books) => {

if (!err)

res.json(books);

else {

console.log(err)

res.json({});

}

});

}

else res.json({}); // empty query

});

Page 668: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 668

app.use( '/api', (req, res) => {

var query = {};

if (req.query.title)

query.title = { $regex : req.query.title };

if (req.query.name)

query['authors.name'] = { $regex: req.query.name };

if (req.query.year)

query.year = req.query.year;

if (Object.keys(query).length != 0) {

Book.find( query, (err, books) => {

if (!err)

res.json(books);

else {

console.log(err)

res.json({});

}

});

}

else res.json({}); // empty query

});

Page 669: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 669

[ { _id: 5967e069359c635bb106d662,

title: 'Intro to Java Programming',

year: 2017,

__v: 0,

authors: [ [Object] ]},

{ _id: 5967d3b75d1bcf5afa49ca3a,

title: 'JavaScript Programming',

year: 2017,

__v: 0,

authors: [ [Object], [Object] ] },

{ _id: 5967e095359c635bb106d665,

title: 'The Art of Computer Programming',

year: 1968,

__v: 0,

authors: [ [Object] ] } ]

Page 670: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 670

[ { _id: 5967e069359c635bb106d662,

title: 'Intro to Java Programming',

year: 2017,

__v: 0,

authors: [ [Object] ]},

{ _id: 5967d3b75d1bcf5afa49ca3a,

title: 'JavaScript Programming',

year: 2017,

__v: 0,

authors: [ [Object], [Object] ] },

{ _id: 5967e095359c635bb106d665,

title: 'The Art of Computer Programming',

year: 1968,

__v: 0,

authors: [ [Object] ] } ]

Page 671: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 671

[ { _id: 5967e069359c635bb106d662,

title: 'Intro to Java Programming',

year: 2017,

__v: 0,

authors: [ [Object] ]},

{ _id: 5967d3b75d1bcf5afa49ca3a,

title: 'JavaScript Programming',

year: 2017,

__v: 0,

authors: [ [Object], [Object] ] },

{ _id: 5967e095359c635bb106d665,

title: 'The Art of Computer Programming',

year: 1968,

__v: 0,

authors: [ [Object] ] } ]

Page 672: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 672

app.use( '/api', (req, res) => {

var query = {};

if (req.query.title)

query.title = { $regex : req.query.title };

if (req.query.name)

query['authors.name'] = { $regex: req.query.name };

if (req.query.year)

query.year = req.query.year;

if (Object.keys(query).length != 0) {

Book.find( query, (err, books) => {

if (!err)

res.json(books);

else {

console.log(err)

res.json({});

}

});

}

else res.json({}); // empty query

});

Page 673: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 673

app.use( '/api', (req, res) => {

var query = {};

if (req.query.title)

query.title = { $regex : req.query.title };

if (req.query.name)

query['authors.name'] = { $regex: req.query.name };

if (req.query.year)

query.year = req.query.year;

if (Object.keys(query).length != 0) {

Book.find( query, (err, books) => {

if (!err)

res.json(books);

else {

console.log(err)

res.json({});

}

});

}

else res.json({}); // empty query

});

Page 674: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 674

app.use( '/api', (req, res) => {

var query = {};

if (req.query.title)

query.title = { $regex : req.query.title };

if (req.query.name)

query['authors.name'] = { $regex: req.query.name };

if (req.query.year)

query.year = req.query.year;

if (Object.keys(query).length != 0) {

Book.find( query, (err, books) => {

if (!err)

res.json(books);

else {

console.log(err)

res.json({});

}

});

}

else res.json({}); // empty query

});

Page 675: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 675

Page 676: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 676

Page 677: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 677

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<title>Book Finder</title>

<script src="jquery.js"></script>

</head>

<body>

<form>

Title: <input name='title'><br>

Author: <input name='author'><br>

Year: <input name='year'><br>

</form>

<ul>

<div id='results'></div>

</ul>

<script>

// next slide -->

</script>

</body>

</html>

Page 678: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 678

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<title>Book Finder</title>

<script src="jquery.js"></script>

</head>

<body>

<form>

Title: <input name='title'><br>

Author: <input name='author'><br>

Year: <input name='year'><br>

</form>

<ul>

<div id='results'></div>

</ul>

<script>

// next slide -->

</script>

</body>

</html>

Page 679: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 679

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<title>Book Finder</title>

<script src="jquery.js"></script>

</head>

<body>

<form>

Title: <input name='title'><br>

Author: <input name='author'><br>

Year: <input name='year'><br>

</form>

<ul>

<div id='results'></div>

</ul>

<script>

// next slide -->

</script>

</body>

</html>

Page 680: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 680

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<title>Book Finder</title>

<script src="jquery.js"></script>

</head>

<body>

<form>

Title: <input name='title'><br>

Author: <input name='author'><br>

Year: <input name='year'><br>

</form>

<ul>

<div id='results'></div>

</ul>

<script>

// next slide -->

</script>

</body>

</html>

Page 681: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 681

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<title>Book Finder</title>

<script src="jquery.js"></script>

</head>

<body>

<form>

Title: <input name='title'><br>

Author: <input name='author'><br>

Year: <input name='year'><br>

</form>

<ul>

<div id='results'></div>

</ul>

<script>

// next slide -->

</script>

</body>

</html>

Page 682: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 682

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<title>Book Finder</title>

<script src="jquery.js"></script>

</head>

<body>

<form>

Title: <input name='title'><br>

Author: <input name='author'><br>

Year: <input name='year'><br>

</form>

<ul>

<div id='results'></div>

</ul>

<script>

// next slide -->

</script>

</body>

</html>

Page 683: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 683

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<title>Book Finder</title>

<script src="jquery.js"></script>

</head>

<body>

<form>

Title: <input name='title'><br>

Author: <input name='author'><br>

Year: <input name='year'><br>

</form>

<ul>

<div id='results'></div>

</ul>

<script>

// next slide -->

</script>

</body>

</html>

Page 684: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 684

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 685: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 685

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 686: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 686

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 687: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 687

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 688: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 688

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 689: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 689

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 690: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 690

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 691: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 691

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 692: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 692

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 693: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 693

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 694: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 694

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 695: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 695

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 696: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 696

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 697: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 697

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 698: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 698

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 699: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 699

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 700: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 700

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 701: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 701

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 702: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 702

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<title>Book Finder</title>

<script src="jquery.js"></script>

</head>

<body>

<form>

Title: <input name='title'><br>

Author: <input name='author'><br>

Year: <input name='year'><br>

</form>

<ul>

<div id='results'></div>

</ul>

<script>

// next slide -->

</script>

</body>

</html>

Page 703: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 703

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<title>Book Finder</title>

<script src="jquery.js"></script>

</head>

<body>

<form>

Title: <input name='title'><br>

Author: <input name='author'><br>

Year: <input name='year'><br>

</form>

<ul>

<div id='results'></div>

</ul>

<script>

// next slide -->

</script>

</body>

</html>

Page 704: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 704

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 705: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 705

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 706: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 706

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 707: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 707

[ { _id: 5967e069359c635bb106d662,

title: 'Intro to Java Programming',

year: 2017,

__v: 0,

authors: [ [Object] ]},

{ _id: 5967d3b75d1bcf5afa49ca3a,

title: 'JavaScript Programming',

year: 2017,

__v: 0,

authors: [ [Object], [Object] ] },

{ _id: 5967e095359c635bb106d665,

title: 'The Art of Computer Programming',

year: 1968,

__v: 0,

authors: [ [Object] ] } ]

Page 708: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 708

[ { _id: 5967e069359c635bb106d662,

title: 'Intro to Java Programming',

year: 2017,

__v: 0,

authors: [ [Object] ]},

{ _id: 5967d3b75d1bcf5afa49ca3a,

title: 'JavaScript Programming',

year: 2017,

__v: 0,

authors: [ [Object], [Object] ] },

{ _id: 5967e095359c635bb106d665,

title: 'The Art of Computer Programming',

year: 1968,

__v: 0,

authors: [ [Object] ] } ]

Page 709: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 709

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 710: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 710

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 711: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 711

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 712: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 712

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 713: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 713

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 714: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 714

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 715: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 715

<script>

$("input").on( "change input textInput", () => {

var title = $("input[name='title']").val();

var author = $("input[name='author']").val();

var year = $("input[name='year']").val();

var query = '?';

if (title) query += 'title=' + title + '&';

if (author) query += 'name=' + author + '&';

if (year) query += 'year=' + year;

var url = 'http://localhost:3000/api/' + query;

$.getJSON(url, (books, status) => {

var results = $("#results");

results.html('');

books.forEach( (book) => {

results.append('<li><i>' + book.title + '</i>, ');

book.authors.forEach( (author) => {

if (author.name) results.append(author.name + ', ');

});

results.append(book.year + '</li>');

});

});

});

</script>

Page 716: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy SD4x-4.9 716

Page 717: Video 4.1 Intro to Node.js Environment Setup Chris Murphy · 2019. 6. 19. · • Node.js and Express simplify the development of Web servers to handle HTTP requests and create and

Property of Penn Engineering, Chris Murphy

Summary

• We can use server-side tools such as Node.js, Express, and MongoDB to develop APIs to make data available on the Web

• We can use client-side tools such as jQuery, React.js, and D3.js to access that data and display it in a Web page

SD4x-4.9 717