Question

In: Computer Science

Using node.js, create the following tasks. 1. Set up a server and HTML file server as...

Using node.js, create the following tasks.

1. Set up a server and HTML file server as shown in the videos.

Once you have it successfully running, make the following adjustments

A. When a 404 error (file not found) occurs, display a funny message about the file missing and/or did you forget how to type?

B. If the user enters a request for the home page (index.html) then:

Display an index.html page you have created which includes your name, course number, course name, assignment name, assignment due date. Place these items on a table. Make the page looks professional and clean by adding some CSS to control formatting, color, font, and other issues.

If the user enters a request for the page (login.html) then:

Display a login.html page which includes a form to request the user enter a userid and password. Also, include a "forgot my password" link. Use the same css as you used previously for format the page. This page will not do anything. But it should include HTML code to make sure the user entered items in each textbox. It should also hide the password when entered.

If the user enters any of the page request, then the 404 error information should display.

What to turn in:

1. Screenshots showing the code, the displaying of the 404 message when a wrong page is entered, the display of the index.html page by the server, and the display of the login.html page by the server.

Solutions

Expert Solution

Step 1 — Creating a Basic HTTP Server

Let’s start by creating a server that returns plain text to the user. This will cover the key concepts required to set up a server, which will provide the foundation necessary to return more complex data formats like JSON.

First, we need to set up an accessible coding environment to do our exercises, as well as the others in the article. In the terminal, create a folder called first-servers:

 
  • mkdir first-servers   

Step 2 — Returning Different Types of Content

The response we return from a web server can take a variety of formats. JSON and HTML were mentioned before, and we can also return other text formats like XML and CSV. Finally, web servers can return non-text data like PDFs, zipped files, audio, and video.

In this article, in addition to the plain text we just returned, you’ll learn how to return the following types of data:

  • JSON
  • CSV
  • HTML

Serving HTML

HTML, HyperText Markup Language, is the most common format to use when we want users to interact with our server via a web browser. It was created to structure web content. Web browsers are built to display HTML content, as well as any styles we add with CSS, another front-end web technology that allows us to change the aesthetics of our websites.

Let’s reopen html.js with our text editor:

nano html.js

Step 3 — Serving an HTML Page From a File

We can serve HTML as strings in Node.js to the user, but it’s preferable that we load HTML files and serve their content. This way, as the HTML file grows we don’t have to maintain long strings in our Node.js code, keeping it more concise and allowing us to work on each aspect of our website independently. This “separation of concerns” is common in many web development setups, so it’s good to know how to load HTML files to support it in Node.js

To serve HTML files, we load the HTML file with the fs module and use its data when writing our HTTP response.

Step 4 — Managing Routes Using an HTTP Request Object

Most websites we visit or APIs we use usually have more than one endpoint so we can access various resources. A good example would be a book management system, one that might be used in a library. It would not only need to manage book data, but it would also manage author data for cataloguing and searching convenience.

Even though the data for books and authors are related, they are two different objects. In these cases, software developers usually code each object on different endpoints as a way to indicate to the API user what kind of data they are interacting with.

Let’s create a new server for a small library, which will return two different types of data. If the user goes to our server’s address at /books, they will receive a list of books in JSON. If they go to /authors, they will receive a list of author information in JSON.


Related Solutions

Create by using Linux server • Create a file name it foo.txt • Remove all permissions...
Create by using Linux server • Create a file name it foo.txt • Remove all permissions from foo.txt • What happen if you try to read the file? • Change foo.txt permission to read and write only for owner • Change foo.txt permission to read for group • Change foo.txt permission to read and write everyone
Create a cronjob which does the following tasks 1.) create a file named <yourname>.txt 2.) in...
Create a cronjob which does the following tasks 1.) create a file named <yourname>.txt 2.) in the 35th minute of the hour change the permission to 755 3.) Create a shell script named first.sh in which you print your name and redirect the output by 45th minute of the hour to the text file
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.
How to do in C++ HTML Converter Create a program that reads an HTML file and...
How to do in C++ HTML Converter Create a program that reads an HTML file and converts it to plain text. Console HTML Converter Grocery List * Eggs * Milk * Butter Specifications Your instructor should provide an HTML file named groceries.html that contains these HTML tags: <h1>Grocery List</h1> <ul>     <li>Eggs</li>     <li>Milk</li>     <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add...
1. Create one XML data file and one DTD file for the entire data set (using...
1. Create one XML data file and one DTD file for the entire data set (using a subset of SQL assignment – see below). [Use of ID and IDREF are not required.] 2. Write and execute XML statements for the following two queries: Q1. Find the name of an employee who lives in Lincoln and works in Omaha. Q2. Find salaries of employees who live in the same cities as the companies for which they work. [You can replace one...
Please provide HTML code for the following: - Create a page that lists a set of...
Please provide HTML code for the following: - Create a page that lists a set of audio files and shows their duration - Create a page that lists a set of video files and plays a different video when you click on the play icon
For a Linux server: • how to Set up a print server (CUPS), allow clients to...
For a Linux server: • how to Set up a print server (CUPS), allow clients to connect, print to PDF • Create a script that backs up the printed documents every 6 hours please can you help me
This is C++ Create a program that reads an HTML file and converts it to plain...
This is C++ Create a program that reads an HTML file and converts it to plain text. Console: HTML Converter Grocery List * Eggs * Milk * Butter Specifications: The HTML file named groceries.html contains these HTML tags: <h1>Grocery List</h1> <ul> <li>Eggs</li> <li>Milk</li> <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add asterisks (*) before the list items, and display the...
Exercise 1 (a) Create a HTML file that uses createElement and appendChild to dynamically insert three...
Exercise 1 (a) Create a HTML file that uses createElement and appendChild to dynamically insert three paragraphs when a button is clicked. (b) Create a HTML file that includes JavaScript that is similar to: let recs = [“my item …1”,”my item…2”, …] i.e. an array that contains several CSV item records. When the user clicks a button, each array element will be rendered as a list element. Use a HTML list. Use createElement and appendChild. (c) Redo your createTable code...
take the following html code and make it work for html validator. heres the ,html file...
take the following html code and make it work for html validator. heres the ,html file <!DOCTYPE html> <html lang="en">    <head>        <title> GettingStarted</title>        <meta charset="utf-8">        <link href="Style.css" rel="stylesheet">    </head>       <body>        <header><h1>GettingStarted</h1></header>        <nav>               <b>        <a href="Home.html">Home</a>&nbsp;        <a href="GettingStarted.html">Getting Started</a>&nbsp;        <a href="MaterialsNeeded.html">Materials Needed</a>&nbsp;                      <a href="TroubleShooting.html">TroubleShooting</a>&nbsp;        <a href="InfoMaterials.html">Infomation on materials</a>&nbsp;   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT