Creating a JavaScript Fullstack NODE.js Web Application
Learning outcomes:
Create a JavaScript Fullstack NODE.js Web application using Express.js and Body Parser from NPMJS.com.
Create a Full Stack JavaScript Web Application which presents the User with an HTML Page with a Form. User will complete this form and click submit.
The app will send back to user an HTML page showing the values that user entered into the Form.
This is set the stage for the next activity, Lab Part II in which students transact the from data into a MONGO database.
Activity: Creating a JavaScript Fullstack NODE.js Web Application
In this lab activity, you will learn how to create a Full Stack JavaScript Web Application using Express.js and Body Parser from npmjs.com.
You will build an application that presents the user with an HTML page containing a form. When the user submits the form, the application will send back an HTML page displaying the values entered by the user.
Prerequisites:
Basic understanding of HTML, CSS, and JavaScript
Familiarity with Node.js and npm (Node Package Manager)
Installation of Node.js and npm on your machine
Requirements:
Node.js and npm installed on your machine
A code editor of your choice (e.g., Visual Studio Code)
Activity Steps:
Step 1: Setup
Create a new directory for your project and navigate to it using the terminal or command prompt.
Initialize a new Node.js project by running the following command:
npm init -y
This command will generate a package.json file with default values.
Step 2: Install Dependencies
Install Express.js and Body Parser by running the following command:
npm install express body-parser
This will install Express.js and Body Parser from npmjs.com and add them as dependencies to your project.
Step 3: Create Project Files
Create a new file named app.js in your project directory.
This route handler extracts the values entered in the form fields (name and email) from the request body and sends an HTML response displaying the submitted values.
Step 7: Start the Server
Add the following code at the end of app.js to start the server:
const port = 3000; app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`); });
Step 8: Run the Application
Open your terminal or command prompt and navigate to the project directory.
Run the following command to start the application:
Copy code
node app.js
Open your web browser and visit http://localhost:3000.
Fill in the form fields with some values and click the "Submit" button.
You should see a new page displaying the submitted values.
Congratulations! You have successfully created a Full Stack JavaScript Web Application using Express.js and Body Parser. In the next activity (Lab Part II), you will learn how to store the form data into a MongoDB database.