Build a full CRUD, RESTful API using Express and Postgres for a Todo List.
- Create all of your necessary app files in this directory. (I have included GitHub's standard Node.js
.gitignoretemplate so that you don't end up pushingnode_modulesto GitHub. - You should
npm install express pg knex. Use express to build your API. Use knex to create your database migrations and seeds. Use pg to connect your express API to your database. - Your core app logic must be separated into the appropriate files, and your project files should be organized into folders according to their purpose: models, controllers, and/or routes.
Each endpoint should respond with the appropriate JSON response. Our API should support:
| HTTP Verb | PATH | Description |
|---|---|---|
| GET | /todos | A route to see all todos |
| GET | /todos/:id | A route to see details about an individual todo item |
| POST | /todos | Create a todo |
| PUT/PATCH | /todos/:id | The ability to update the description of a todo |
| PUT/PATCH | /todos/:id/complete | The ability to mark a todo complete |
| DELETE | /todos/:id | Delete a todo |
In order to make your application work both locally and when deployed, you will need to have configurations that works for both your local machine and in Heroku. On the Heroku servers, there will automatically some variables that do not existing locally. These variables exist on the process.env object.
In your main server file, update the line of code where you declare your port to:
const PORT = process.env.PORT || 8000; // Or whichever port you choose for your local serverThis allows your application to set the port number to Heroku's production port. Since this variable is undefined locally, it will be set to 8000 (or whichever port number you choose) when you are developing on you personal computer.
If your application is running on Heroku, there will be a variable called process.env.NODE_ENV with a value of 'production'. If your are on you local machine, then this variable will be undefined. Depending on which environment you are in, you'll need to use a different pg connection object. We can check with a basic ternary operator like so:
const { Pool } = require('pg')
const connectionDevelopment = {
database: 'todo', // Or replace this with your database name
user: 'postgres', // If you have a different postgres user, replace here
password: '', // If you have a postgres password, write it here
host: 'localhost',
port: 5432
}
const connectionProduction = {
connectionString: process.env.DATABASE_URL,
ssl: {rejectUnauthorized: false}
}
const pool = new Pool(process.env.NODE_ENV === 'production' ? connectionProduction : connectionDevelopment)The exports of your knexfile.js should have two properties, one for development and one for production, like so:
module.exports = {
development: {
client: 'pg',
connection: {
database: 'todo', // Or replace this with your database name
user: 'postgres',
password: ''
}
},
production: {
client: 'pg',
connection: process.env.DATABASE_URL
}
};Deploy Your Project to Heroku and submit the link on Canvas. Provide the URL to your github repo as a comment in your submission. When your app is deployed, be sure to test that is works by making API calls to it.
You can read this tutorial on deploying an Express app with Postgres on Heroku and running knex migrations and seeds.