You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -39,7 +39,7 @@ The `form` attributes define how to communicate with the server:
39
39
-`action`: The resource/URL where data is sent for processing when the form is submitted. If this is not set, or is an empty string, the form will be submitted back to the current page URL
40
40
-`method`: Defines the HTTP method to use (`POST` or `GET`).
41
41
42
-
`POST` is generally more secure because it keeps sensitive information out of the URL, which means they won't show up in server logs, and is the standard choice for creating or updating data on the server side. `GET` is for forms that don't modify data, such as search forms, or when you want the form submission to be bookmarkable or shareable via URL.
42
+
`POST` is generally more secure because it keeps sensitive information out of the URL, which means they won't show up in server logs, and is the standard choice for creating or updating data on the server side. `GET` is for forms that don't modify data, such as search forms, or when you want the form submission to be bookmarkable or shareable via URL. The form data here is sent as a query string as part of the request url.
43
43
44
44
#### Form handling process
45
45
@@ -51,16 +51,16 @@ We then generate a new or updated view with the controller's response and redire
51
51
52
52
Before the data from a form is sent off to our server, we should consider two important steps:
53
53
54
-
-*Validation* ensures user input meets the specified criteria (e.g. required fields, correct format).
55
-
-*Sanitization* cleans user input to prevent malicious data from being processed by removing or encoding potentially malicious characters.
54
+
-_Validation_ ensures user input meets the specified criteria (e.g. required fields, correct format).
55
+
-_Sanitization_ cleans user input to prevent malicious data from being processed by removing or encoding potentially malicious characters.
56
56
57
57
We don't always have to sanitize data right when we get it - sometimes it makes sense to sanitize just before we use it instead.
58
58
59
59
We'll be using a library called `express-validator` to help us out with both of these. While it makes these processes much simpler, it's important to understand the underlying concepts of these two operations.
60
60
61
61
#### Installing express-validator
62
62
63
-
We start as usual by installing the correct package in the *root* folder of our project.
63
+
We start as usual by installing the correct package in the _root_ folder of our project.
64
64
65
65
```bash
66
66
npm install express-validator
@@ -82,7 +82,7 @@ The `body()` function allows you to specify which fields in the request body sho
82
82
[
83
83
body("birthdate", "Must be a valid date.")
84
84
.optional({ values:"falsy" })
85
-
.isISO8601() // Enforce a YYYY-MM-DD format.
85
+
.isISO8601(),// Enforce a YYYY-MM-DD format.
86
86
];
87
87
```
88
88
@@ -99,7 +99,7 @@ You can also chain multiple validation methods, with unique error messages if th
99
99
.notEmpty()
100
100
.withMessage("Name can not be empty.")
101
101
.isAlpha()
102
-
.withMessage("Name must only contain alphabet letters."),
102
+
.withMessage("Name must only contain alphabet letters."),
103
103
];
104
104
```
105
105
@@ -122,11 +122,15 @@ When unescaped, this would be rendered into HTML as:
122
122
123
123
```html
124
124
<div>
125
-
About Me: <script>alert("Hacked!");</script>!
125
+
About Me:
126
+
<script>
127
+
alert("Hacked!");
128
+
</script>
129
+
!
126
130
</div>
127
131
```
128
132
129
-
To prevent this [cross-site scripting (XSS) attack](https://en.wikipedia.org/wiki/Cross-site_scripting), we can *escape* the output (you may also see this referred to as *encoding*). Escaped HTML replaces special characters, like `<`, with their respective HTML entities, in this case `<`. In EJS, we can escape the output using `<%= %>`.
133
+
To prevent this [cross-site scripting (XSS) attack](https://en.wikipedia.org/wiki/Cross-site_scripting), we can _escape_ the output (you may also see this referred to as _encoding_). Escaped HTML replaces special characters, like `<`, with their respective HTML entities, in this case `<`. In EJS, we can escape the output using `<%= %>`.
130
134
131
135
```ejs
132
136
<div>
@@ -191,7 +195,7 @@ In our form, the action would look something like this:
`/users/:id/update` is an *endpoint* we've created on our Express server.
198
+
`/users/:id/update` is an _endpoint_ we've created on our Express server.
195
199
196
200
### Putting it together
197
201
@@ -220,7 +224,7 @@ const PORT = process.env.PORT || 3000;
220
224
app.listen(PORT, () =>console.log(`Express app listening on port ${PORT}!`));
221
225
```
222
226
223
-
Most simple forms will use the `Content-Type: application/x-www-form-urlencoded` HTTP header when sending data to the server. Express, however, can't natively parse that data. We can use the `express.urlencoded()` middleware to handle this for us and automatically set form's data to the `req.body` field. When `extended` is `false`, our server will only accept a `string` or an array of data, so we set it to `true` for some added flexibility. Note that if the `Content-Type` doesn't match `application/x-www-form-urlencoded`, then your server will show the data as an empty object `{}`.
227
+
Most simple forms will use the `Content-Type: application/x-www-form-urlencoded` HTTP header when sending data to the server. Express, however, can't natively parse that data. We can use the `express.urlencoded()` middleware to handle this for us and automatically set form's data to the `req.body` field. When `extended` is `false`, our server will only accept a `string` or an array of data, so we set it to `true` for some added flexibility. Note that if the `Content-Type` doesn't match `application/x-www-form-urlencoded`, then your server will show the data as an empty object `{}`.
224
228
225
229
Let's create a new router called `usersRouter.js` in the routes folder:
@@ -541,11 +551,10 @@ Don't forget to update the view to display these new fields!
541
551
542
552
What if we want to search for a specific user in a list of thousands? We'll need a new route and view that lets clients search our list of users.
543
553
544
-
1. Add a form (in `createUser.ejs` or another view) which accepts a `name` or `email` (or both!)
545
-
1. Create a new route `/search` which accepts `GET` and `POST` requests.
546
-
1. Add the search logic to your controller which searches your list for a matching user.
547
-
- Your `POST` request should handle searching for the user.
548
-
- Your `GET` request should then render the search result.
554
+
1. Add a form with a `GET` method (in `createUser.ejs` or another view) which accepts a `name` or `email` (or both!)
555
+
1. Create a new route `/search` which accepts a `GET` request.
556
+
1. Add the search logic to your controller which searches your list for a matching user. Form data that has been sent via a `GET` request will not be available in the `req.body`. You will need to use `req.query` instead.
557
+
- Your `GET` request should handle searching for the user and then render the search result.
549
558
1. Display the search results in a new view: `search.ejs`.
0 commit comments