Resources, Routing, and MVC

Model

Operations and routes


/* Display all toys (read) */
app.get('/toys', (req, res) => {
    // ...
})

/* Create a new toy */
app.post('/toys', (req, res) => {
    // ...
})

/* Display a form to create a new toy */
app.get('/toys/new', (req, res) => {
    // ...
})

/* Display (read) details for one toy. 
   :id represents a "route parameter" */
app.get('/toys/:id', (req, res) => {
    // ...
})

/* Edit(update) a toy */
app.post('/toys/:id', (req, res) => {
   // ..
})

/* Display a form to edit (update) a toy */
app.get('/toys/:id/edit', (req, res) => {
    // ...
})

Controller

/* Display all toys */
app.get('/toys', (req, res) => {
    toyController.index(req, res)
})

/* Create a new toy */
app.post('/toys', (req, res) => {
    toyController.create(req, res)
})

/* Display a form to create a new toy */
app.get('/toys/new', (req, res) => {
    toyController.newToy(req, res)
})

/* Display details for one toy.  
   :id represents a "route parameter" */
app.get('/toys/:id', (req, res) => {
    toyController.show(req, res)
})

/* Edit a toy */
app.get('/toys/:id/edit', (req, res) => {
    toyController.edit(req, res)
})

/* Update a toy */
app.post('/toys/:id', (req, res) => {
    toyController.update(req, res)
})

View

Database

Putting it all Together

Database

SQLite

db = new sqlite3.Database(__dirname + '/toys.sqlite')
db.run('CREATE TABLE Toys (id INTEGER PRIMARY KEY, name TEXT NOT NULL, description TEXT NOT NULL, manufacturer TEXT NOT NULL, price REAL NOT NULL);')

Callbacks / Promises