- Tokenr
- Play
- Scoreboard
Word:
Definition:
[ 236 ] www.it-ebooks.info ChapterLevel:
Add
We will post to the /new route when the form is submitted We have an input element for the word and its definition (we could use a text area here, but an input element will encourage a short definition) Then, we have a set of radio buttons for choosing the level Since this will post to the same route, we need a POST route on the server side to catch the new word: app.post('/new', function (req, res) { if (req.user && req.user.admin) { var w = { word: req.body.word.toLowerCase(), definition: req.body.definition, level: parseInt(req.body.level, 10) }; words.find({ word: w.word }, function (err, ws) { if (ws.length === 0) { words.insert(w); } }); res.redirect('/new'); } else { res.redirect('/'); } }); If there's an admin user logged in, we'll create a word object, w Then, we'll check the word's database to see if the word already exists; if it doesn't, we'll insert it Finally, we'll return to the form so that the administrator can insert another word if they want to Finally, let's add this path to the navigation, but only when an administrator is logged in In the nav.ejs file of the views directory, add this as the last list item: