In this exercise we're going to build a simple NodeJS / Express app that uses cookies to remember the name of visitors to our app.
- Create an empty directory & repository
- Run
npm init -y
- Run
npm install --save express cookie-parser
- Run
npm install --save-dev nodemon
- Put your express app in
app.js
- Start your app with
nodemon app.js
- Open another terminal window
- On the homepage, render a form that asks for
Name
with a single text input - When you submit that form it should
POST
to/set-name
- Within the
POST
/set-name
route, take the posted name and save it to a cookie namedusername
I had to use body-parser
in order to eaisly get the username value from the form data, but with that I just the middlewears interface, res.cookie('cookieName': value)
followed by .send()
. Easy peasy.
Oh and the form needed to be embeded thusly:
<form method="post" action="/set-name">