Create an Express application that includes a custom controller and associated routes, demonstrating the MVC architectural pattern. This exercise is designed to practice organizing application logic through controllers and managing client requests via routes.
-
Project Setup:
- Initialize a new Node.js project and install Express.
- Create an
index.js
file to define your Express app logic. - Create a
server.js
file to import your Express app and start the server.
-
Controller Implementation:
- Create a new directory named
controllers
. - Inside the
controllers
directory, create a file namedtodoController.js
. - Implement a simple controller with functions:
listTodos
(to list all todo items) andcreateTodo
(to add a new todo item). - Simulate data storage with an array of todos in memory.
- Create a new directory named
-
Setup Routes:
- Create a new directory named
routes
. - Inside the
routes
directory, create a file namedtodoRoutes.js
. - Define routes for listing todos (
GET /todos
) and creating a new todo (POST /todos
).
- Create a new directory named
-
Integrate Routes into Express App:
- In your
index.js
, import the routes and use them with your Express app instance.
- In your
-
Server Configuration:
- In
server.js
, import your Express app fromindex.js
, configure it to listen on port 3000.
- In
- id: Unique identifier (integer or string).
- title: Title of the todo item (string).
- completed: Completion status (boolean).
- Use a global array in
todoController.js
to hold todo items. - Implement CRUD operations interacting with this array.
- Test the
GET /todos
andPOST /todos
routes with appropriate data. - Ensure your tests verify the correct structure and behavior of the todo items.
- Install Requirements
- We have to start with installing our test packages as development dependencies
- The packages we want to use are
jest
andsupertest
- We can install packages as dev dependencies by including a
--save-dev
flag when running the install command (likenpm i --save-dev <your packages>
) - With this information, what would the command to install
jest
andsupertest
as dev dependencies look like?
- Update the package.json scripts
- If you've done things right, you should have a package.json file in your project with
express
andcors
as dependencies, andjest
andsupertest
as dev dependencies. - In the scripts section of the package.json, we want to update our test script to run
jest
instead of the defaultecho whatever and all that
- If you've done things right, you should have a package.json file in your project with
- Run the tests
- From here, if all has gone well, you can run the tests by running
npm test
- From here, if all has gone well, you can run the tests by running
- Validate Correctness
- Make sure that the tests pass, examine each test case and ensure that everything is passing, and if not, that you understand the errors that are happening and have a sense of how to fix it
- Extend the controller with additional functionalities, such as updating and deleting todo items.
- Implement validation in your
createTodo
function.
- Push your completed project to a GitHub repository.
- Ensure your
package.json
includes Express as a dependency. - Submit the URL to your GitHub repository.