Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Lab Exercise 1: Setting Up Your First Express Server

1. Create a new directory for your project and navigate into it.
2. Initialize a new npm project and create a package.json file.
3. Install Express.js using npm.
4. Create an app.js file and write a simple Express server that listens on port 3000.
5. Start the server and visit http://localhost:3000 in your browser to check that the
server is running.

Lab Exercise 2: Basic Routing

1. Extend your Express server application from Lab Exercise 1.


2. Add routes that respond to the following HTTP methods and paths:
o A GET request to / that sends back 'Hello, world!'
o A POST request to /about that sends back 'About Us'
o A PUT request to /contact that sends back 'Contact Information'
3. Test your routes using a tool like Postman or by creating HTML forms that make
these requests.

Lab Exercise 3: Using Middleware

1. Add middleware to your application that logs the request method and URL to the
console every time a request is received.
2. Create custom middleware that blocks access to a new route /secret by sending
a response with a status code of 403 and a message 'Access Denied'.
3. Test your middleware to ensure it is functioning correctly.

Lab Exercise 4: Handling Route Parameters

1. Create a new route that uses route parameters to capture a user ID and article ID
from the URL. The format should be /users/:userId/articles/:articleId.
2. Modify the route handler to send back a message that includes both the user ID
and article ID extracted from the URL.
3. Test this by visiting different URLs that match this pattern in your browser.

Lab Exercise 5: Implementing Query Parameters

1. Modify your Express server application to include a new route /search.


2. Within the /search route, implement functionality to capture query parameters, for
instance, ?name=John&age=30.
3. Write the route handler so that it responds with a message incorporating the
query parameters, e.g., "Searching for John, age 30".
4. Ensure your server properly decodes the query parameters and handles cases
where no parameters are provided.
5. Test this new functionality by sending different requests from your browser or
using a tool like Postman, modifying the query parameters each time to see the
different responses generated by your server.

You might also like