0% found this document useful (0 votes)
40 views64 pages

Getting Data: - Query Parameter - Get - Post

The document discusses how HTTP request objects can contain query parameters and POST data sent from a client. Query parameters are key-value pairs included in the URL, while POST data is sent in the request body. The request object properties req.query and req.params can be used to access query parameters and route parameters respectively.

Uploaded by

Syed Ifran Ejaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views64 pages

Getting Data: - Query Parameter - Get - Post

The document discusses how HTTP request objects can contain query parameters and POST data sent from a client. Query parameters are key-value pairs included in the URL, while POST data is sent in the request body. The request object properties req.query and req.params can be used to access query parameters and route parameters respectively.

Uploaded by

Syed Ifran Ejaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 64

Getting Data

• Query Parameter
• GET
• POST
2

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

Property of Penn Engineering, Chris Murphy SD4x-4.4


3

Request Object Query Properties


• An HTTP Request object can include query
properties that come from the URL
http://localhost:3000/?name=Lydia&location=United+States

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

var query = req.query;


console.log(query);

var name = query.name;

// 'Lydia'
var location =
query.location; // 'United
res.send('Hello
States' World!');
});
var length =
Object.keys(query).length;
Property of Penn Engineering, Chris Murphy SD4x-4.4
4

Request Object Query Properties


• An HTTP Request object can include query
properties that come from the URL
http://localhost:3000/?name=Lydia&location=United+States

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

var query = req.query;


console.log(query);

var name = query.name;

// 'Lydia'
var location =
query.location; // 'United
res.send('Hello
States' World!');
});
var length =
Object.keys(query).length;
Property of Penn Engineering, Chris Murphy SD4x-4.4
5

Request Object Query Properties


• An HTTP Request object can include query
properties that come from the URL
http://localhost:3000/?name=Lydia&location=United+States

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

Property of Penn Engineering, Chris Murphy SD4x-4.4


6

Request Object Query Properties


• An HTTP Request object can include query
properties that come from the URL
http://localhost:3000/?name=Lydia&location=United+States

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

Property of Penn Engineering, Chris Murphy SD4x-4.4


7

Request Object Query Properties


• An HTTP Request object can include query
properties that come from the URL
http://localhost:3000/?name=Lydia&location=United+States

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

Property of Penn Engineering, Chris Murphy SD4x-4.4


8

Request Object Query Properties


• An HTTP Request object can include query
properties that come from the URL
http://localhost:3000/?name=Lydia&location=United+States

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

var query = req.query;


console.log(query);

var name = query.name;

// 'Lydia'
var location =
query.location; // 'United
res.send('Hello
States' World!');
});
var length =
Object.keys(query).length;
Property of Penn Engineering, Chris Murphy SD4x-4.4
9

Request Object Query Properties


• An HTTP Request object can include query
properties that come from the URL
http://localhost:3000/?name=Lydia&location=United+States

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

var query = req.query;


console.log(query);

var name = query.name;

// 'Lydia'
var location =
query.location; // 'United
res.send('Hello
States' World!');
});
var length =
Object.keys(query).length;
Property of Penn Engineering, Chris Murphy SD4x-4.4
10

Request Object Query Properties


• An HTTP Request object can include query
properties that come from the URL
http://localhost:3000/?name=Lydia&location=United+States

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

var query = req.query;


console.log(query);

var name = query.name;

// 'Lydia'
var location =
query.location; // 'United
res.send('Hello
States' World!');
});
var length =
Object.keys(query).length;
Property of Penn Engineering, Chris Murphy SD4x-4.4
11

Request Object Query Properties


• An HTTP Request object can include query
properties that come from the URL
http://localhost:3000/?name=Lydia&location=United+States

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

var query = req.query;


console.log(query);

var name = query.name;

// 'Lydia'
var location =
query.location; // 'United
res.send('Hello
States' World!');
});
var length =
Object.keys(query).length;
Property of Penn Engineering, Chris Murphy SD4x-4.4
12

Request Object Query Properties


• An HTTP Request object can include query
properties that come from the URL
http://localhost:3000/?name=Lydia&location=United+States

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

var query = req.query;


console.log(query);

var name = query.name;

// 'Lydia'
var location =
query.location; // 'United
res.send('Hello
States' World!');
});
var length =
Object.keys(query).length;
Property of Penn Engineering, Chris Murphy SD4x-4.4
13

Request Object Query Properties


• An HTTP Request object can include query
properties that come from the URL
http://localhost:3000/?name=Lydia&location=United+States

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

var query = req.query;


console.log(query);

var name = query.name;

// 'Lydia'
var location =
query.location; // 'United
res.send('Hello
States' World!');
});
var length =
Object.keys(query).length;
Property of Penn Engineering, Chris Murphy SD4x-4.4
14

Request Object Query Properties


• An HTTP Request object can include query
properties that come from the URL
http://localhost:3000/?name=Lydia&location=United+States

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

var query = req.query;


console.log(query);

var name = query.name;

// 'Lydia'
var location =
query.location; // 'United
res.send('Hello
States' World!');
});
var length =
Object.keys(query).length;
Property of Penn Engineering, Chris Murphy SD4x-4.4
15

Request Object Query Properties


• An HTTP Request object can include query
properties that come from the URL
http://localhost:3000/?name=Lydia&location=United+States

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

var query = req.query;


console.log(query);

var name = query.name;

// 'Lydia'
var location =
query.location; // 'United
res.send('Hello
States' World!');
});
var length =
Object.keys(query).length;
Property of Penn Engineering, Chris Murphy SD4x-4.4
16

Request Object Query Properties


• An HTTP Request object can include query
properties that come from the URL
http://localhost:3000/?name=Lydia&location=United+States

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

var query = req.query;


console.log(query);

var name = query.name;

// 'Lydia'
var location =
query.location; // 'United
res.send('Hello
States' World!');
});
var length =
Object.keys(query).length;
Property of Penn Engineering, Chris Murphy SD4x-4.4
17

Request Object Parameters


• An HTTP Request object can include param
properties that come from a parameterized URL
http://localhost:3000/name/Lydia/location/United States
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 res.send('Hello World!');


length = Object.keys(params).length; // 2
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


18

Request Object Parameters


• An HTTP Request object can include param
properties that come from a parameterized URL
http://localhost:3000/name/Lydia/location/United States
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 res.send('Hello World!');


length = Object.keys(params).length; // 2
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


19

Request Object Parameters


• An HTTP Request object can include param
properties that come from a parameterized URL
http://localhost:3000/name/Lydia/location/United States
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 res.send('Hello World!');


length = Object.keys(params).length; // 2
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


20

Request Object Parameters


• An HTTP Request object can include param
properties that come from a parameterized URL
http://localhost:3000/name/Lydia/location/United States
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 res.send('Hello World!');


length = Object.keys(params).length; // 2
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


21

Request Object Parameters


• An HTTP Request object can include param
properties that come from a parameterized URL
http://localhost:3000/name/Lydia/location/United States
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 res.send('Hello World!');


length = Object.keys(params).length; // 2
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


22

Request Object Parameters


• An HTTP Request object can include param
properties that come from a parameterized URL
http://localhost:3000/name/Lydia/location/United States
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 res.send('Hello World!');


length = Object.keys(params).length; // 2
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


23

Request Object Parameters


• An HTTP Request object can include param
properties that come from a parameterized URL
http://localhost:3000/name/Lydia/location/United States
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 res.send('Hello World!');


length = Object.keys(params).length; // 2
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


24

Request Object Parameters


• An HTTP Request object can include param
properties that come from a parameterized URL
http://localhost:3000/name/Lydia/location/United States
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!');
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


25

Request Object Parameters


• An HTTP Request object can include param
properties that come from a parameterized URL
http://localhost:3000/name/Lydia/location/United States
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 res.send('Hello World!');


length = Object.keys(params).length; // 2
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


26

Request Object Parameters


• An HTTP Request object can include param
properties that come from a parameterized URL
http://localhost:3000/name/Lydia/location/United States
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 res.send('Hello World!');


length = Object.keys(params).length; // 2
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


27

Request Object Parameters


• An HTTP Request object can include param
properties that come from a parameterized URL
http://localhost:3000/name/Lydia/location/United States
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 res.send('Hello World!');


length = Object.keys(params).length; // 2
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


28

Request Object Parameters


• An HTTP Request object can include param
properties that come from a parameterized URL
http://localhost:3000/name/Lydia/location/United States
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 res.send('Hello World!');


length = Object.keys(params).length; // 2
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


29

Request Object Parameters


• An HTTP Request object can include param
properties that come from a parameterized URL
http://localhost:3000/name/Lydia/location/United States
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 res.send('Hello World!');


length = Object.keys(params).length; // 2
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


30

Request Object Parameters


• An HTTP Request object can include param
properties that come from a parameterized URL
http://localhost:3000/name/Lydia/location/United States
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 res.send('Hello World!');


length = Object.keys(params).length; // 2
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


31

Request Object Parameters


• An HTTP Request object can include param
properties that come from a parameterized URL
http://localhost:3000/name/Lydia/location/United States
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 res.send('Hello World!');


length = Object.keys(params).length; // 2
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


32

Request Object Parameters


• An HTTP Request object can include param
properties that come from a parameterized URL
http://localhost:3000/name/Lydia/location/United States
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 res.send('Hello World!');


length = Object.keys(params).length; // 2
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


33

Request Object Parameters


• An HTTP Request object can include param
properties that come from a parameterized URL
http://localhost:3000/name/Lydia/location/United States
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 res.send('Hello World!');


length = Object.keys(params).length; // 2
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


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

Property of Penn Engineering, Chris Murphy SD4x-4.4 219


35

Property of Penn Engineering, Chris Murphy SD4x-4.4


36

Property of Penn Engineering, Chris Murphy SD4x-4.4


37

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

Property of Penn Engineering, Chris Murphy SD4x-4.4


38

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

Property of Penn Engineering, Chris Murphy SD4x-4.4


39

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

Property of Penn Engineering, Chris Murphy SD4x-4.4


40

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

Property of Penn Engineering, Chris Murphy SD4x-4.4


41

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

Property of Penn Engineering, Chris Murphy SD4x-4.4


42

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

Property of Penn Engineering, Chris Murphy SD4x-4.4


43

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

Property of Penn Engineering, Chris Murphy SD4x-4.4


44

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

Property of Penn Engineering, Chris Murphy SD4x-4.4


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


Property of Penn Engineering, Chris Murphy SD4x-4.4 230
46

var express = require('express');


var app = express();

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

var bodyParser = require('body-


parser');
app.use(bodyParser.urlencoded({ e
xtended: true }));

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


var name = req.body.username;
var animals = req.body.animal; // this
is an array
app.listen(3000,
. . . () => {
console.log('Listening
res.send('Thank you!'); on port 3000');
});
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


47

var express = require('express');


var app = express();

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

var bodyParser = require('body-


parser');
app.use(bodyParser.urlencoded({ e
xtended: true }));

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


var name = req.body.username;
var animals = req.body.animal; // this
is an array
app.listen(3000,
. . . () => {
console.log('Listening
res.send('Thank you!'); on port 3000');
});
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


48

var express = require('express');


var app = express();

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

var bodyParser = require('body-


parser');
app.use(bodyParser.urlencoded({ e
xtended: true }));

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


var name = req.body.username;
var animals = req.body.animal; // this
is an array
app.listen(3000,
. . . () => {
console.log('Listening
res.send('Thank you!'); on port 3000');
});
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


49

var express = require('express');


var app = express();

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

var bodyParser = require('body-


parser');
app.use(bodyParser.urlencoded({ e
xtended: true }));

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


var name = req.body.username;
var animals = req.body.animal; // this
is an array
app.listen(3000,
. . . () => {
console.log('Listening
res.send('Thank you!'); on port 3000');
});
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


50

var express = require('express');


var app = express();

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

var bodyParser = require('body-


parser');
app.use(bodyParser.urlencoded({ e
xtended: true }));

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


var name = req.body.username;
var animals = req.body.animal; // this
is an array
app.listen(3000,
. . . () => {
console.log('Listening
res.send('Thank you!'); on port 3000');
});
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


51

var express = require('express');


var app = express();

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

var bodyParser = require('body-


parser');
app.use(bodyParser.urlencoded({ e
xtended: true }));

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


var name = req.body.username;
var animals = req.body.animal; // this
is an array
app.listen(3000,
. . . () => {
console.log('Listening
res.send('Thank you!'); on port 3000');
});
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


52

var express = require('express');


var app = express();

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

var bodyParser = require('body-


parser');
app.use(bodyParser.urlencoded({ e
xtended: true }));

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


var name = req.body.username;
var animals = req.body.animal; // this
is an array
app.listen(3000,
. . . () => {
console.log('Listening
res.send('Thank you!'); on port 3000');
});
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


53

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

Property of Penn Engineering, Chris Murphy SD4x-4.4


54

var express = require('express');


var app = express();

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

var bodyParser = require('body-


parser');
app.use(bodyParser.urlencoded({ e
xtended: true }));

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


var name = req.body.username;
var animals = req.body.animal; // this
is an array
app.listen(3000,
. . . () => {
console.log('Listening
res.send('Thank you!'); on port 3000');
});
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


55

var express = require('express');


var app = express();

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

var bodyParser = require('body-


parser');
app.use(bodyParser.urlencoded({ e
xtended: true }));

app.use('/handleForm', (req, res)


=> {
var name = req.body.username;
var animals = req.body.animal;
app.listen(3000, () => {
// this is an array
console.log('Listening
. . . on port 3000');
});res.send('Thank you!');
});
Property of Penn Engineering, Chris Murphy SD4x-4.4
56

var express = require('express');


var app = express();

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

var bodyParser = require('body-


parser');
app.use(bodyParser.urlencoded({ e
xtended: true }));

app.use('/handleForm', (req, res)


=> {
var name = req.body.username;
var animals = req.body.animal;
app.listen(3000, () => {
// this is an array
console.log('Listening
. . . on port 3000');
});res.send('Thank you!');
});
Property of Penn Engineering, Chris Murphy SD4x-4.4
57

var express = require('express');


var app = express();

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

var bodyParser = require('body-


parser');
app.use(bodyParser.urlencoded({ e
xtended: true }));

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


var name = req.body.username;
var animals = req.body.animal; // this
is an array
app.listen(3000,
. . . () => {
console.log('Listening
res.send('Thank you!'); on port 3000');
});
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


58

var express = require('express');


var app = express();

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

var bodyParser = require('body-


parser');
app.use(bodyParser.urlencoded({ e
xtended: true }));

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


var name = req.body.username;
var animals = req.body.animal; // this
is an array
app.listen(3000,
. . . () => {
console.log('Listening
res.send('Thank you!'); on port 3000');
});
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


59

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

Property of Penn Engineering, Chris Murphy SD4x-4.4


60

var express = require('express');


var app = express();

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

var bodyParser = require('body-


parser');
app.use(bodyParser.urlencoded({ e
xtended: true }));

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


var name = req.body.username;
var animals = req.body.animal; // this
is an array
app.listen(3000,
. . . () => {
console.log('Listening
res.send('Thank you!'); on port 3000');
});
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


61

var express = require('express');


var app = express();

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

var bodyParser = require('body-


parser');
app.use(bodyParser.urlencoded({ e
xtended: true }));

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


var name = req.body.username;
var animals = req.body.animal; // this
is an array
app.listen(3000,
. . . () => {
console.log('Listening
res.send('Thank you!'); on port 3000');
});
});

Property of Penn Engineering, Chris Murphy SD4x-4.4


62

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

Property of Penn Engineering, Chris Murphy SD4x-4.4


63

var express = require('express');


var app = express();

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

var bodyParser = require('body-


parser');
app.use(bodyParser.urlencoded({ e => {
xtended: true }));
// this is an array
app.use('/handleForm', (req, res)
var name = req.body.username;
var animals =
req.body.animal;
. . .
res.send('Thank you!');
});

app.listen(3000, () => { SD4x-4.4


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

Property of Penn Engineering, Chris Murphy SD4x-4.4 249

You might also like