The end goal of the exercise is to implement the following routes:
POST /api/snippetsto create a snippetGET /api/snippetsto get a list of snippetsGET /api/snippets/:idto get a single snippet
We will create the snippet routes in a different file, api/snippets.js, which will export an Express Router.
That will look something like this:
// Contents of api/snippets.js
import express from "express";
const router = express.Router();
// GET /api/snippets
router.get("/", async (request, response) => {
// TODO
});
// TODO: POST /api/snippets
// TODO: GET /api/snippets/:id
export default router;We will also have the database connection in a separate file:
// Contents of database.js
import knex from "knex";
const knexInstance = knex({
client: "mysql2",
connection: {
host: process.env.DB_HOST || "127.0.0.1",
port: process.env.DB_PORT || 3306,
user: process.env.DB_USER || "root",
password: process.env.DB_PASSWORD || "my-secret-pw",
database: process.env.DB_NAME || "hyf_node_week2",
},
});
export default knexInstance;At this point verify that your project structure looks like this:
- api
- snippets.js
- app.js
- database.js
- package.json