Question: Instructions Create a folder in your repository named static to store the files for this assignment. Inside the static folder create another folder named public

Instructions

Create a folder in your repository named static to store the files for this assignment. Inside the static folder create another folder named public to hold the files that will be made available to browsers on the public Internet.

Use npm to install the st module.

npm install st 

The following code shows one way to use the st module to serve requests for files.

const http = require('http'); const st = require('st'); const port = 8000; var mount = st({ path: 'public/', index: 'index.html' }); const server = http.createServer((req, res) => { mount(req, res); }); server.listen(port, () => { console.log(`Server running at http://localhost:${port}/`); }); 

Place the above code in a file named server.js.

Add content to the public folder and go to http://localhost:8000/ to test that the server is working correctly. In particular, create index.html and see that this file is served for the following 2 different URLs. This is accomplished by including index: 'index.html' in the configuration object.

http://localhost:8000/ http://localhost:8000/index.html 

problem 1

Include an img element in one of your pages that refers to an image file in the public folder.

Use an external style sheet to style the web pages.

Test that browsers load and render the Web page as expected.

problem 2

Intercept requests for the path /hi and return the following html instead of letting st handle the request.

You requested /hi.

The url property of the req object passed into your request handler provides the path requested by the user. You can use this in an if statement as follows.

if (req.url === '/hi') { // ... } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!