Controller
We have a database always doing the same thing. We want to replace this with a "Controller". A Controller is going to simplify the access to the database.
You may ask, why do we have a controller in there? Why isn't express directly acting on the database?
Quickly, 3 reasons:
What if we wanted to access the database from outside Express? Maybe through the command line, or maybe through another method of communication
You want to simplify the SQL, reduce it to the part you need, and expose just that. It makes understanding what each function does much easier
What if you wanted some day to throw SQLite and use MongoDB instead? No problem. Keep everything as it is, just change the internals of the controller
Instead of the Javascript above, replace db.js
with the following:
and index.js
with:
We temporarily comment out the express part, because we just want to test our database.
We want for our database to finish initializing before we pursue, so we'd want to use await
here.
Run the project (npm start
), you should see a list of people.
Last updated
Was this helpful?