Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

To create a Node.js server that serves Klingon jokes and a front - end HTML page with a button to fetch and display a new

To create a Node.js server that serves Klingon jokes and a front-end HTML page with a button to fetch and display a new joke, follow these steps:
1.**Set up the project**: Make sure you have Node.js and npm installed.
2.**Create the project directory and initialize**:
```bash
mkdir klingon-jokes-app
cd klingon-jokes-app
npm init -y
npm install -g express body-parser
```
3.**Create the main application file (`app.js`)**:
```javascript
const express = require('express');
const app = express();
const port =3000;
// List of Klingon jokes
const jokes =[
"A Klingon, a Ferengi, and a Betazoid woman were sitting around in Quark's bar...",
"Q: How many Klingons does it take to change a light bulb? A: None. Klingons aren't afraid of the dark.",
"Klingons do not make software bugs. Their software is perfect from the first compile.",
"Klingon programming does not tolerate error messages."
];
// Middleware to serve static files (HTML, CSS, JS)
app.use(express.static('public'));
// API endpoint to get a random joke
app.get('/joke',(req, res)=>{
const randomIndex = Math.floor(Math.random()* jokes.length);
const joke = jokes[randomIndex];
res.json({ joke });
});
app.listen(port,()=>{
console.log(`App listening at http://localhost:${port}`);
});
```
4.**Create the public directory and HTML file**:
```bash
mkdir public
cd public
```
5.**Create the HTML file (`index.html`)**:
```html

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

1. Explain goal-setting theory.

Answered: 1 week ago

Question

Under what conditions are two qualitative variables independent?

Answered: 1 week ago