From 96d3ae552a10b3f87f3dc438d9ede9769ad2afb7 Mon Sep 17 00:00:00 2001 From: Jet Hughes Date: Wed, 21 Sep 2022 11:54:30 +1200 Subject: [PATCH] vault backup: 2022-09-21 11:54:30 --- content/notes/10-routes-controllers.md | 46 ++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/content/notes/10-routes-controllers.md b/content/notes/10-routes-controllers.md index cec560324..86f854e25 100644 --- a/content/notes/10-routes-controllers.md +++ b/content/notes/10-routes-controllers.md @@ -54,4 +54,50 @@ app.use(cookieParser()) - end the req-res cycle - call next middleware function in stack +## forms +- in html — a collection of elements inside
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 +
+ + + +
+``` + +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 + ![](https://i.imgur.com/H1BXggu.png)