Question

In: Computer Science

Write a simple javascript program using express and node.js to create a simple webpage that can...

Write a simple javascript program using express and node.js to create a simple webpage that can lead to other pages within the program. if possible, Comment everything out so I could understand how every things works.  But basically, server should be hosted on localhost:8000 and should have a simple starting page. Maybe a welcome message, with at least two "links" that goes to a "homepage" and then a "exit" page.

Solutions

Expert Solution

Answer 1) Below is the javascript program using express and node.js. Below is app.js

// importing the express module

const express = require("express");

const app = express();

// when request is send to home page this returns the home.html

// That is when we type http://localhost:3000/ in URL of browser

app.get("/", function (req, res) {

  res.sendFile(__dirname + "/home.html");

});

// When request to contact page that is http://localhost:3000/contact in the URL

app.get("/contact", function (req, res) {

  res.sendFile(__dirname + "/contact.html");

});

// When request to about page that is http://localhost:3000/about in the URL

app.get("/about", function (req, res) {

  res.sendFile(__dirname + "/about.html");

});

// listen for request.

app.listen(3000, function () {

  console.log("server started at port 3000");

});

Code for home page is below.

<!DOCTYPE html>

<html lang="en">

  <head>

    <meta charset="UTF-8" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <title>This is the home page</title>

  </head>

  <body>

    <h1>This is the Home Page of the Application.</h1>

    <h3>Welcome to the Online Aplication</h3>

    <button onclick="contactRedirect()">contact page</button>

    <button onclick="aboutRedirect()">about page</button>

    <script>

      

      function contactRedirect() {

        window.location.replace("http://localhost:3000/contact");

      }

      function aboutRedirect(){

        window.location.replace("http://localhost:3000/about");

      }

    </script>

  </body>

</html>

code for contact page

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Contact Page</title>

</head>

<body>

    <h1>This is the contact Page.</h1>

    <h3>Contact me at satwindersinghsaini</h3>

    <address>

        62 West Street

        New York

    </address>

</body>

</html>

code for about.html page

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>About Page</title>

</head>

<body>    

   <h1> I am Currently a student of computer Science engineering</h1>

   <h4>My hobbies are playing cricket, table tennis and coding.</h4>

</body>

</html>

CODE SCREENSHOTS

1) app.js

2) home.html

3) contact.html

4) about.html

OUTPUT SCREEN SHOTS

1) home page screen shot

2) contact page screen shot



Related Solutions

Using Node.js with Express, show how you would create the following routes, using these URL's, as...
Using Node.js with Express, show how you would create the following routes, using these URL's, as examples: ~ http:#localhost:4200/books/isbn/978123487999/author This URL will return an author's full name from array of names, mapped by ISBN. ~ http:〃localhost:4200/books This returns all the books (just the book names) from the database.
JavaScript Create a welcome message on your webpage using three variables At least two of the...
JavaScript Create a welcome message on your webpage using three variables At least two of the variables should be concatenated together Use document.write to add a statement about what is your favorite past time? Format the HTML document appropriately with a title, and the correct HTML structure to hold the JavaScript Create an array to share what your favorite color is. The array must have at least three values and be called via the index value Correctly link the JavaScript...
Objective: Create a webpage with a simple quiz. Instructions: The webpage must contain at least ten...
Objective: Create a webpage with a simple quiz. Instructions: The webpage must contain at least ten simple questions as a quiz. The user must be able to answer the questions in the input boxes and finally click on a Submit button to get a final score. The questions must be such a way that some answer formats must cover all the following types: A number A string An expression (e.g. 2+3) If the user does not answer a question and...
Write a javascript program according to the follow requirements: Create a function that converts Fahrenheit to...
Write a javascript program according to the follow requirements: Create a function that converts Fahrenheit to Celsius. It takes a single argument which represents degrees in Fahrenheit. It converts it and returns the degrees in Celsius. Create another function that converts Celsius to Fahrenheit. It takes a argument in Celsius and returns the degrees in Fahrenheit. Implement the function convert(isFtoC, from, to) below. It takes the following three arguments: isFtoC: a boolean that is true if degrees must be converted...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to display the current day and time. b) Write a JavaScript program to print the contents of the current window.   c) Write a JavaScript program where the program takes a random integer between 1 to 10 d) Write a JavaScript program to calculate multiplication and division of two numbers (input from the user). e)Write a JavaScript program to create a new string from a given...
Using JavaScript, create a simple web page that will take orders for Chess Org. Chess Org...
Using JavaScript, create a simple web page that will take orders for Chess Org. Chess Org sells yearly membership for $30 each. Discounts are given for buying in bulk (see Table 1 and example below). Table 1 – Discounts Quantity Purchased Discount 1 – 29 0% 30 - 59 10% 60 + 15% Example: If a customer purchases 40 family membership, their total price (CTotal) would be: COriginalPrice = 30*40=1200 DMainDiscount = 1200*0.1=120 CTotalPrice = COriginalPrice - DMainDiscount= 1200-120 =...
Using java you have to create a simple program that will allow for the storage of...
Using java you have to create a simple program that will allow for the storage of requests for money. Each request will consist of a person's name and dollar amount (US dollars). The business rules are straight forward. Each name should be a first and last name. The first letter of each element in the name should be capitalized. The dollar amount should be between 1 and 1000 dollars and include cents (2 decimals). You should include validations, but it...
Please create an Event-Based JavaScript Program of your choice. Please write new code for this project...
Please create an Event-Based JavaScript Program of your choice. Please write new code for this project (starting from scratch). Thank you!!
write a Program in C++ Using a structure (struct) for a timeType, create a program to...
write a Program in C++ Using a structure (struct) for a timeType, create a program to read in 2 times into structures, and call the method addTime, in the format: t3 = addTime(t1, t2); Make sure to use add the code to reset and carry, when adding 2 times. Also, display the resultant time using a function: display(t3);
USING JAVASCRIPT Create a file name dayOfWeek.js and write an arrow function named dayOfWeek that accepts...
USING JAVASCRIPT Create a file name dayOfWeek.js and write an arrow function named dayOfWeek that accepts a Date object dateStr and returns a string that is the day of the week in English form (i.e. “Sunday”, “Monday”, etc.). Test your function by creating a date object that is a significant date to you (such as your birthday) and passing that date object to your function. Test your function at least twice with two different dates. Submit the dayOfWeek.js file to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT