mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-27 14:54:05 -06:00
vault backup: 2022-09-21 11:54:30
This commit is contained in:
parent
8e8c2f6cc9
commit
96d3ae552a
@ -54,4 +54,50 @@ app.use(cookieParser())
|
|||||||
- end the req-res cycle
|
- end the req-res cycle
|
||||||
- call next middleware function in stack
|
- call next middleware function in stack
|
||||||
|
|
||||||
|
## forms
|
||||||
|
- in html — a collection of elements inside <form></form> tags, containing a submit element
|
||||||
|
- used to collect user input
|
||||||
|
- flexible - can colect many different types of input
|
||||||
|
- secure - can send data in post requests with cross site request forgery protection
|
||||||
|
|
||||||
|
e.g.,
|
||||||
|
``` html
|
||||||
|
<form action="/login" method="post">
|
||||||
|
<label for="username">Username</label>
|
||||||
|
<input id="username" type="text" name="username_field" value="">
|
||||||
|
<input tupe="submit" value="OK">
|
||||||
|
</form>
|
||||||
|
```
|
||||||
|
|
||||||
|
sequence
|
||||||
|
- display form
|
||||||
|
- blank or prepopulated fields
|
||||||
|
- recieve data from user in HTTP post request
|
||||||
|
- validate and sanitze data
|
||||||
|
- if invalid - redisplay form with error messages, and user populated fields
|
||||||
|
- if valid - perform required actions
|
||||||
|
- redirect user to other page
|
||||||
|
|
||||||
|
### validation and sanitization
|
||||||
|
- required fields
|
||||||
|
- correct format etc
|
||||||
|
- remov/replace malicious input
|
||||||
|
- can use express-validator module
|
||||||
|
|
||||||
|
``` js
|
||||||
|
const {body, validationResult} = require('express-validator');
|
||||||
|
|
||||||
|
[
|
||||||
|
//...
|
||||||
|
body('name', "Empty name")
|
||||||
|
.trim() //remove whitespace
|
||||||
|
.isLength({min: 1}) //check length
|
||||||
|
.escape(), //escape potentially dangerous chars
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
### form routes
|
||||||
|
- router.get() - serve page
|
||||||
|
- router.post() - process request
|
||||||
|
|
||||||

|

|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user