Lab 1
Build a Basic Express.js Server
Scenario
You have been hired as a freelance developer for a new local coffee shop, “The Daily Grind.” They need a simple website to establish their online presence, but first, they need a backend server to serve the web pages. Your task is to build a basic web server using Node.js and Express that can serve their homepage and a contact page.
Learning Objectives
By the end of this activity, you will have demonstrated your ability to:
- Set up a Node.js project and manage dependencies.
- Create a basic Express.js server.
- Implement routing to handle specific URL requests.
- Serve static HTML files in response to requests.
Instructions
Task 1: Project Setup
- Create a new directory for your project (e.g.,
daily-grind-server). - Navigate into the new directory in your terminal.
- Initialize a new Node.js project by running
npm init -y. This will create yourpackage.jsonfile. - Install
expressas a project dependency by runningnpm install express.
Task 2: Create the Web Pages
- Inside your project directory, create a new folder named
public. - Inside the
publicfolder, create two HTML files:index.html: This will be the homepage. Add a simple heading like<h1>Welcome to The Daily Grind!</h1>.contact.html: This will be the contact page. Add a heading like<h1>Contact Us</h1>and some placeholder text.
Task 3: Build the Express Server
- In the root of your project directory, create a file named
server.js. - Inside
server.js, write the code for your Express server. It must do the following:- Import the
expresslibrary. - Import the built-in
pathmodule, which will help you create correct file paths. - Create an instance of an Express application.
- Define a port to run the server on (e.g.,
3000). - Create a route handler for
GETrequests to the root URL (/). When this route is requested, it should send theindex.htmlfile from yourpublicdirectory. - Create another route handler for
GETrequests to/contact. This should send thecontact.htmlfile. - Start the server and have it listen on your chosen port. When it starts, it should log a message to the console, like
Server is running on port 3000.
- Import the
Hint: To send a file, you’ll need to provide an absolute path. Use path.join(__dirname, 'public/index.html') to create a reliable path to your HTML files.
Submission Instructions
- Ensure all your files (
package.json,server.js,public/index.html,public/contact.html) are in the correct locations. - Verify that your server runs without errors by executing
node server.jsin your terminal. - Test your routes by visiting
http://localhost:3000/andhttp://localhost:3000/contactin your browser. - Once complete, submit a link to a GitHub repository containing your project files.
Grading Rubric
| Criteria | Description | Points |
|---|---|---|
| Project Setup | 10 points | |
package.json | package.json is correctly initialized and express is listed as a dependency. | 5 |
| File Structure | Project has the correct file structure (server.js, public/, HTML files). | 5 |
| Server Configuration | 10 points | |
| Server Creation | server.js successfully creates an Express application instance. | 5 |
| Port Listening | Server listens on a specified port and logs a confirmation message on startup. | 5 |
| Routing | 20 points | |
Home Route (/) | A GET route for / is correctly implemented. | 5 |
| Home Page Served | The / route successfully serves the index.html file. | 5 |
Contact Route (/contact) | A GET route for /contact is correctly implemented. | 5 |
| Contact Page Served | The /contact route successfully serves the contact.html file. | 5 |
| Total | 40 |
Reflection Questions
After completing this activity, consider the following:
- What is the difference between
res.send()andres.sendFile()? When would you use one over the other? - Why is the
pathmodule necessary when serving files? What could go wrong if you just used a relative path like'public/index.html'? - How would you add a third page (e.g., a menu page) to this server? What steps would you take?