Create a basic Express server that handles two routes: /
and /about
. This exercise ranks 1 point worth of difficulty, with an expected work time of an hour.
- Initialize a new Node.js project with
npm init
. - Install Express using
npm install express
. - Create an
index.js
file where your server code will reside.
- Route
/
: Should send back a response with the text: "Welcome to my first Express server!". - Route
/about
: Should send back a JSON response containing an object with a name and an age. Hint.
- A server.js file is provided. The expectation is to import the express app from index.js and have the express app listen from server.js
- Once you've got the listener set up, you should run
node server.js
to have a copy of your server running
- Server runs without errors on port 3000.
- Navigating to
http://localhost:3000/
displays "Welcome to my first Express server!". - Navigating to
http://localhost:3000/about
displays a JSON response with a name and age.
This exercise includes automated tests to verify the functionality of your Express server using Jest and Supertest.
Ensure all project dependencies are installed:
npm install jest supertest
Execute the tests with the following command:
npm test
- Passing tests are marked with a green check (✓), indicating your server meets the requirements.
- Failing tests are marked with a red cross (✕), with details provided to help you identify and correct the issue.
If you encounter issues:
- Confirm all dependencies are installed with
npm install
. - Make sure your server exports the Express
app
for testing. - Review Jest's error messages for insights on needed corrections.