10-exercise lab workbook designed to reinforce key concepts in Node.js, JavaScript, and web development.
Here's a comprehensive 10-exercise lab workbook designed to reinforce key concepts in Node.js, JavaScript, and web development. Each exercise includes learning outcomes, detailed workflows, and fully specified code.
Lab Workbook: Advanced Web Development with Node.js and JavaScript
Learning Outcomes
By completing this lab workbook, students will be able to:
Set up and configure a Node.js project using npm
Create and manage Express.js servers with routes and middleware
Implement GET and POST requests in both client-side and server-side JavaScript
Manipulate the DOM using JavaScript
Work with asynchronous JavaScript using Promises and async/await
Implement form handling and data validation
Use the Fetch API for making HTTP requests
Apply CSS selectors and styling to web pages
Create and use JavaScript modules
Implement basic error handling in Node.js applications
Exercise 1: Setting Up a Node.js Project
Learning Outcomes
Initialize a Node.js project using npm
Understand the purpose of package.json
Install and use external dependencies
Workflow
Create a new directory for your project
Initialize a new Node.js project
Install Express.js as a dependency
Create a basic server file
Code
mkdir my-node-project
cd my-node-project
npm init -y
npm install express
Create a file named server.js:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.get('/help', (req, res) => {
res.send('Welcome to the help desk!');
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
Run the server:
bash
Copy
node server.js
Exercise 2 demonstrates HTML forms that interact with the server endpoints.
Exercise 2: Creating Routes in Express.js with HTML Forms
Learning Outcomes
Define routes in Express.js
Handle different HTTP methods (GET, POST)
Use route parameters
Create HTML forms to interact with server endpoints
We useexpress.urlencoded({ extended: true }) middleware to parse form data.
We serve static files from the public directory using express.static.
The root route (/) serves the index.html file.
The /api/users POST route to create a new user with an auto-incremented ID.
The /api/users/:id GET route searches for a user in the users array and returns a 404 if not found.
We've created an index.html file with forms and buttons to interact with our API endpoints:
A button to get all users
A form to get a user by ID
A form to add a new user
The HTML file includes JavaScript to handle form submissions and button clicks, making fetch requests to our API endpoints and displaying the results.
This exercise provides a full-circle example of how to create Express.js routes and interact with them using HTML forms and JavaScript. Students can run the server and open http://localhost:3000 in their browser to see the forms and interact with the API endpoints directly.
Exercise 3: Implementing Middleware with File Logging
Example : Making Custom Middleware
Learning Outcomes
Understand the concept of middleware in Express.js
Implement custom middleware functions
Use built-in and third-party middleware
Implement file logging for server activities
Workflow
Create a custom logging middleware that writes to a file
Use the built-in express.static middleware
Implement error-handling middleware with file logging
const startMessage = `${new Date().toISOString()} - Server started and running at http://localhost:${port}\n`;
accessLogStream.write(startMessage);
console.log(startMessage.trim());
});
This modification ensures that all server activities, including requests, errors, and the server start event, are logged to both the console and a file named access.log. This approach provides a persistent record of server activities, which is useful for debugging and monitoring purposes.