-Express Router Intro
-Express Router and Middleware
-Introducing Cookies
-Sending Cookies
-Cookies Parser Middleware
-Signing Cookies
Express Router Intro
Express Router and Middleware
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
res.send("All dogs")
})
router.get('/:id', (req, res) => {
res.send("Viewing one dogs")
})
router.get('/:id/edit', (req, res) => {
res.send("Editing one dogs")
})
module.exports = router;
express router is used to break up the code into separate files, like little pieces. It may be easier to put middleware between the smaller routes as well.
Introducing Cookies
Cookies are bits of information that are stored in a users browser when browsing a website.
Once a cookie is set, a user browser will send the cookie on every request to the site.
Cookies allow use to make HTTP stateful. It is just a key and a value pair.
Cookies are used to remember information about some user and to show relevant content to a user. Cookies are a unique identifier for a specific user.
Sending Cookies
Using express to set a cookie or to retrieve cookies from an incoming request
const express = require('express');
const app = express();
app.get('/greet', (req, res) => {
res.send("welcome")
}
app.get('/setname', (req, res) => {
res.cookie('name', 'stevie chicks');
res.send('Cookie sent');
})
app.listen(3000, () => {
console.log("Serving");
Cookies Parser Middleware
This will parse cookies within express.
https://www.npmjs.com/package/cookie-parser
to install at the terminal
npm i cookie-parser
then add this line of code
const cookieParser = require('cookie-parser');
app.use(cookieParser());
Signing Cookies
Signing cookies is about making sure that the original data that was sent to the client browser is still the data that is being sent back to the server. Cryptography is used to ensure the integrity of the signed cookie data.
Top comments (0)