insert.run('Obi Wan Kenobi', "That’s no moon!" );
insert.run('The Terminator', 'Hasta la vista, baby.');
insert.run("Captain Ahab", "There she blows!");
Now let’s create a way to make new quotes. First, we’ll need an endpoint that accepts requests with the data for new quotes, in server.js
:
app.post('/quotes', (req, res) => {
const { quote, author } = req.body;
const insert = database.prepare('INSERT INTO quotes (name, quote) VALUES (?, ?)');
insert.run(author, quote);
res.redirect('/');
});
This tells Express to accept requests at /quotes
, then parses and destructures the request body into two variables: quote
and author
. We use these two variables in a prepared statement that inserts a new row into the quotes
database. After that, we redirect to the homepage, where the list of quotes (which we’ll build in a moment) can render the new item.
Update the views/index.pug
to include a form, like so: