Share
Explore

CSD 3103 - NPM Node Application Development Lab Test 1

CSD 3103 - NPM Node Application Development Lab Test

Objective:

Develop a Node.js application that serves a random programming joke from a JSON file and integrates with a front-end HTML page to display the joke. Additionally, include basic CSS for styling.

Instructions:

Set up the Project:
Ensure Node.js and npm are installed.
Create a new directory for your project and initialize it with npm init -y.
Install necessary packages: express and body-parser.
bash
Copy code
mkdir programming-jokes-app
cd programming-jokes-app
npm init -y
npm install express body-parser

Create the main application file (app.js):
Set up an Express server that serves static files and an API endpoint to fetch a random joke from a JSON file.
Include middleware to handle JSON data.

image.png
Create the Jokes JSON file (jokes.json):
Include an array of programming jokes in JSON format.
[
"Why do programmers prefer dark mode? Because light attracts bugs.",
"How many programmers does it take to change a light bulb? None. It's a hardware problem.",
"Why do Java developers wear glasses? Because they can't C#.",
"A SQL query walks into a bar, walks up to two tables and asks: 'Can I join you?'"
]
Create the Public Directory and HTML File:
Create a public directory and inside it, create an index.html file with a button to fetch a new joke and a paragraph to display it.
Add basic CSS styling for a better user interface.
image.png
Create the Client-side JavaScript (public/script.js):
Add functionality to fetch a joke from the server and display it when the button is clicked.
image.png
Create the CSS file (public/style.css):
Add basic styling for the HTML elements.
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}

button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}

#jokeDisplay {
margin-top: 20px;
font-size: 18px;
color: #333;
}
Run the Application:
Start the server and test your application.
bash
Copy code
node app.js

Test the Application:
Open your web browser and navigate to http://localhost:3000.
Click the "Get a New Joke" button to fetch and display a random programming joke.

Submission:

Documentation: Create a Word document including:
Screenshots of your Visual Studio Code panels and terminal showing the server running.
Screenshots of your browser displaying the jokes.
Copy-paste your code for app.js, jokes.json, index.html, script.js, and style.css.
Name the document StudentName_StudentID.docx.
Upload: Submit the Word document to the class portal.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.