The primary Express server file. The code in this file:
- Configures and runs the Express server
- Sets up routes to handle HTTP requests
- Processes incoming request and sends back a response
Notice we don't interact directly with the database in this file, we delegate
those tasks to the db.mjs
module.
Converts the JavaScript value to JSON and sends it as a response.
See the res.json() documentation for more details.
Converts the incoming request payload (usually via POST
or PUT
) from a JSON
string to a JavaScript value.
See the express.json() documentation for more details.
Responsible for data access to the PostgreSQL database. The code in this file:
- Sets up a database client connection.
- Provides functions that interact with the database.
Remember when we set up the project environment file? We're going to reuse those variables for connecting to the PostgreSQL server from our Express server.
The dotenv
package reads the .env
file and converts those
variables to Node runtime environment variables, which is accessed via
process.env
.
When Node was first introduced, there was no official JavaScript module system. So Node picked the non-standard CommonJS module system. Well, it's 2021 and there's a standard JavaScript module system ("ESM"), so we should be using it.
In order for Node to recognize that we're using ESM, we suffix our modules with
.mjs
instead of .js
. See the Node documentation for more info.