Lab Notebook 1: Building the Level 1 Full Stack Nodejs App
Student Lab Lecture Guide
Introduction
This guide will take you through five progressive exercises to set up an Express.js web server, host static files, use Handlebars as a template engine, create a form, and process form data to log into a text file.
Here, defaultLayout: 'main' tells Express to use main.handlebars as the default layout for the views.
Update the root route (/) to render the home view:
javascriptCopy code
app.get('/', (req, res) => {
res.render('home');
});
The res.render('home'); line tells Express to render the home view (i.e., home.handlebars) using the main layout (i.e., main.handlebars). The content of home.handlebars will replace the {{{body}}} placeholder in main.handlebars.
Save the app.js file and restart your server by running node app.js in the terminal.
Now, when you navigate to http://localhost:3000, you should see the content of home.handlebars rendered inside the HTML structure defined in main.handlebars.
Exercise 4: Set Up a Form with Fields to Gather User Data
Objectives:
Create an HTML form within a Handlebars template to collect user data (username and email).
Steps:
1. Update the home.handlebars File:
Open the home.handlebars file located inside the views directory.
Erase any existing content and insert a form with POST method and fields for username and email. The action attribute should be set to /submit which will be the route handling the form submission.
Below is the content to be placed inside the home.handlebars file:
The <form> tag’s action attribute is set to /submit, meaning the form data will be sent to the /submit URL when the form is submitted.
The method is set to post, which is the HTTP method used to send data to the server.
Two <input> fields collect the username and email, with required attributes ensuring these fields are not left empty.
A <button> submits the form.
2. Handling Form Submission:
Open the app.js file.
Below the existing code, add a new POST route to handle form submissions at the /submit path.
Add the following code to your app.js file:
javascriptCopy code
app.post('/submit', (req, res) => {
const username = req.body.username;
const email = req.body.email;
// Process the username and email here
res.send(`Received the data: ${username}, ${email}`);
});
In this block:
app.post('/submit', (req, res) => {...} creates a new route that listens for POST requests at the /submit URL.
const username = req.body.username; and const email = req.body.email; extract the username and email values from the form data.
res.send(...); sends a response back to the client, confirming receipt of the data.
Note: For the server to correctly parse the incoming request body, ensure that the app.use(express.urlencoded({ extended: true })); middleware is set in your app.js.
Here is how the middleware should be included in app.js:
const express = require('express');
const app = express();
const PORT = 3000;
app.use(express.urlencoded({ extended: true }));
Save the changes to app.js and restart the server with node app.js.
Now, when you navigate to http://localhost:3000, you will see a form. Upon submission, the server will receive the form data and respond with a confirmation message displaying the received username and email.
Exercise 5: Process Form Data and Log to Text File
Objectives:
Process the form data on the server-side.
Log the received form data to a text file (data.txt).
Steps:
1. Update the /submit POST Route in app.js:
In the app.js file, create a /submit POST route.
Utilize express.urlencoded({ extended: true }) middleware to parse the form data.
Inside the /submit route, use the fs.appendFile function to write the form data to a data.txt file.
Here's the complete and updated app.js file:
javascriptCopy code
// app.js
const express = require('express');
const fs = require('fs');
const exphbs = require('express-handlebars');
const app = express();
const PORT = 3000;
// Middleware to serve static files from the 'public' directory
Now, when the form is submitted, the server will process the form data and append it to a data.txt file in the project directory. The user will receive a response stating "Data written to file" confirming the successful operation.
Testing:
Run your server with the command node app.js.
Navigate to http://localhost:3000 in a web browser.
Fill out the form with a username and email and submit it.
Check the data.txt file in your project directory to see the appended form data.
Want to print your doc? This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (