const http = require("http");
const app = http.createServer( (req, res) => {
res.writeHead(200, {ContentType: "text/html"});
res.write("<h1>1 to 1000 Example</h1>");
res.write("<p>");
for(let n = 1; n <= 1000; n++) {
res.write(n + " ");
if (n % 10 == 0) {
res.write("<br>");
}
}
res.write("<p>");
res.end( );
});
app.listen(3000);
console.log("Server listening on Port 3000");
var person = { name: 'Alice', gender: 'F', age: 11 };
A JavaScript object literal is like a Python dictionary, except that the keys don't need quotes.
var groceryItem = { item: "Green Peas", price: 0.98 };
a. Print the price of the item:console.log(groceryItem.price);b. Add this new key-value pair: quantity: 15.
groceryItem.quantity = 15;
var arr = [ {a: "123", b: "234"}, {a: "567", b: "890"} ];
convert arr to a JSON string, then convert the JSON string back to the array
object literals arr2. Answer:
var s = JSON.stringify(arr);
console.log(s);
// Output:
[{"a":"123",b":"234"},{"a":"567","b":"890"}];
var arr2 = JSON.parse(s);
// Output:
[
{a: "123", b: "234"},
{a: "567", b: "890"}
]facweb.cdm.depaul.eduis
216.220.181.39
GET /test.txt http/1.0The response would look something like this:
HTTP/1.1 302 Found Date: Thu, 19 Feb 2021 02:50:55 GMT Server: Apache/2.0.53 HP-UX-Apache-based_Web_Server (Unix) PHP/4.3.8 Last-Modified: Tue, 27 Oct 2015 14:39:09 GMT ETag: "16896-61d-408dd700" Accept-Ranges:Bytes Content-Length: 18 Connection: close Content-Type: text/html Content: This is a test.
PS C:\Users\sjost> Invoke-WebRequest facweb.cdm.depaul.edu/sjost/test.txt
StatusCode : 200
StatusDescription : OK
Content : This is a test.
RawContent : HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 18
Content-Type: text/plain
Date: Fri, 19 Feb 2021 20:50:26 GMT
ETag: "6590583dc510d11:0"
Last-Modified: Tue, 27 Oct 2015 14:39:09 GMT
Serve...
Forms : {}
Headers : {[Accept-Ranges, bytes], [Content-Length, 18],
[Content-Type, text/plain], [Date, Fri,
19 Feb 2021 20:50:26 GMT]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : mshtml.HTMLDocumentClass
RawContentLength : 18
> curl -v http://facweb.cdm.depaul.edu/sjost/test.txtYou should get output something like this:
* Trying 216.220.181.39:80... * TCP_NODELAY set * Connected to facweb.cdm.depaul.edu (216.220.181.39) port 80 (#0) > GET /sjost/test.txt HTTP/1.1 > Host: facweb.cdm.depaul.edu > User-Agent: curl/7.68.0 > Accept: */* > * Mark bundle as not supporting multiuse < HTTP/1.1 200 OK < Content-Type: text/plain < Last-Modified: Tue, 27 Oct 2015 14:39:09 GMT < Accept-Ranges: bytes < ETag: "6590583dc510d11:0" < Server: Microsoft-IIS/10.0 < X-Powered-By: ASP.NET < Date: Mon, 02 Oct 2023 00:25:08 GMT < Content-Length: 18
const PORT = 3000;
const http = require("http");
const app = http.createServer((req, res) => {
res.write(`<p>${req.method}<br>`;
res.write(`${req.url}</p>`);
res.end( );
console.log(req.headers);
});
app.listen(PORT);
Predict the output. Browser output:GET
/
// Object literal output to the Node console:
{
host: 'localhost:3000',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0)
Gecko/20100101 Firefox/109.0',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,
image/avif,image/webp,*/*;q=0.8',
'accept-language': 'en-US,en;q=0.5',
'accept-encoding': 'gzip, deflate, br',
dnt: '1',
connection: 'keep-alive',
'upgrade-insecure-requests': '1',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'none',
'sec-fetch-user': '?1'
}
--------------------------------
URL: localhost:3000/
Displayed webpage:
<html>
<head>
<title>Homepage</title>
</head>
<body>
<h1>Acme Travel Homepage</h1>
<p>Welcome to Acme Travel Agency</p>
</body>
</html>
--------------------------------
URL: localhost:3000/contact_us
Displayed webpage:
<html>
<head>
<title>Contact Us</title>
</head>
<body>
<h1>Contact Acme Travel</h1>
<p>Phone Numbers:<br>
Main: 312/234-5678<br>
Fax: 312/234-5679</p>
</body>
</html>
--------------------------------
URL: localhost:3000/specials
Displayed webpage:
<html>
<head>
<title>Specials</title>
</head>
<body>
<h1>Acme Travel Specials</h1>
<table>
<tr>
<th>City</th>
<th>Tour Length</th>
<th>Price</th>
</tr>
<tr>
<td>Barcelona</td>
<td>5 days</td>
<td>$2095</td>
</tr>
<tr>
<td>London</td>
<td>9 days</td>
<td>$3545</td>
</tr>
<tr>
<td>Paris</td>
<td>13 days</td>
<td>$9965</td>
</tr>
</table>
</body>
</html>
--------------------------------
URL: any other URL other than the three above.
Displayed webpage:
<html>
<head>
<title>Route not Found</title>
</head>
<body>
<h1>404: Page not Found</h1>
<p>Sorry, we couldn't find that page.</p>
</body>
</html>
Write this HTTP header information for each webpage:res.writeHead(200, {"Content-Type": "text/html" });
const PORT = 3000;
const http = require("http");
const app = http.createServer((req, res) => {
res.writeHead(200, {"Content-Type": "text/html" });
var url = req.url;
if (url == "/") {
res.write(`
<html>
<head>
<title>Homepage</title>
</head>
<body>
<h1>Acme Travel Homepage</h1>
<p>Welcome to Acme Travel Agency</p>
</body>
</html>
`);
}
else if(url == "/contact_us") {
res.write(`
<html>
<head>
<title>Contact Us</title>
</head>
<body>
<h1>Contact Acme Travel</h1>
<p>Phone Numbers:<br>
Main: 312/234-5678<br>
Fax: 312/234-5679</p>
</body>
</html>
`);
}
else if(url == "/specials") {
res.write(`
<html>
<head>
<title>Specials</title>
</head>
<body>
<h1>Acme Travel Specials</h1>
<table>
<tr>
<th>City</th>
<th>Tour Length</th>
<th>Price</th>
</tr>
<tr>
<td>Barcelona</td>
<td>5 days</td>
<td>$2095</td>
</tr>
<tr>
<td>London</td>
<td>9 days</td>
<td>$3545</td>
</tr>
<tr>
<td>Paris</td>
<td>13 days</td>
<td>$9965</td>
</tr>
</table>
</body>
</html>
`);
}
else {
res.write(`
<html>
<head>
<title>Route not Found</title>
</head>
<body>
<h1>404: Page not Found</h1>
<p>Sorry, we couldn't find that page.</p>
</body>
</html>
`);
}
res.end( );
});
app.listen(PORT);
const PORT = 3000;
const http = require("http");
const app = http.createServer((req, res) => {
res.writeHead(200, {"Content-Type": "text/html" });
switch(req.url) {
case "/":
res.write(`
<html>
<head>
<title>Homepage</title>
</head>
<body>
<h1>Acme Travel Homepage</h1>
<p>Welcome to Acme Travel Agency</p>
</body>
</html>
`);
break;
case "/contact_us":
res.write(`
<html>
<head>
<title>Contact Us</title>
</head>
<body>
<h1>Contact Acme Travel</h1>
<p>Phone Numbers:<br>
Main: 312/234-5678<br>
Fax: 312/234-5679</p>
</body>
</html>
`);
break;
case "/specials") {
res.write(`
<html>
<head>
<title>Specials</title>
</head>
<body>
<h1>Acme Travel Specials</h1>
<table>
<tr>
<th>City</th>
<th>Tour Length</th>
<th>Price</th>
</tr>
<tr>
<td>Barcelona</td>
<td>5 days</td>
<td>$2095</td>
</tr>
<tr>
<td>London</td>
<td>9 days</td>
<td>$3545</td>
</tr>
<tr>
<td>Paris</td>
<td>13 days</td>
<td>$9965</td>
</tr>
</table>
</body>
</html>
`);
break;
default:
res.write(`
<html>
<head>
<title>Route not Found</title>
</head>
<body>
<h1>404: Page not Found</h1>
<p>Sorry, we couldn't find that page.</p>
</body>
</html>
`);
}
res.end( );
});
app.listen(PORT);
--------------------------------
URL: localhost:3000/specials
Displayed webpage:
<html>
<head>
<title>Specials</title>
</head>
<body>
<h1>Acme Travel Specials</h1>
<table>
<tr>
<th>City</th>
<th>Tour Length</th>
<th>Price</th>
</tr>
<tr>
<td>Barcelona</td>
<td>5 days</td>
<td>$2095</td>
</tr>
<tr>
<td>London</td>
<td>9 days</td>
<td>$3545</td>
</tr>
<tr>
<td>Paris</td>
<td>13 days</td>
<td>$9965</td>
</tr>
</table>
</body>
</html>
--------------------------------
Answer:
const PORT = 3500;
const http = require("http");
const app = http.createServer((req, res) => {
// Set up and use JSON file for specials data.
var data = `[{"city":"Barcelona","length":5,"price":2095},
{"city":"London","length":9,"price":3545},
{"city":"Paris","length":13,"price":2095}]`;
var specials = JSON.parse(data);
if (req.url == "/specials") {
res.writeHead(200, {"Content-Type":"text/html"});
res.write(`
<html>
<head>
<title>Specials</title>
<style>
body { font: 100% helvetica, arial, sans-serif;
background-color: #b0b0ff; }
h1 { color: navy; }
p { color: #404040; }
table { border-collapse: collapse; }
td { border: 2px black solid; }
td, th { padding: 5px; }
</style>
</head>
<body>
<h1>Acme Travel Specials</h1>
<table>
<tr>
<th>City</th>
<th>Tour Length</th>
<th>Price</th>
</tr>
`);
for(let s of specials) {
res.write("<tr>\n");
res.write(`<td>${s.city}</td>`);
res.write(`<td>${s.length}</td>`);
res.write(`<td>$${s.price}</td>`);
res.write("</tr>\n");
}
res.write(`
</table>
</body>
</html>
`);
}
else {
res.writeHead(200, {"Content-Type":"text/html"});
res.write("<p>Route not Found</p>");
}
res.end( );
});
app.listen(PORT);
Reference: W3Schools Node.js File System Module
www.w3schools.com/nodejs/nodejs_filesystem.asp
| Action | fs Function |
|---|---|
| Read from File | readFile |
| Create file | open |
| Write to File | writeFile |
| Append to File | readFile |
| Delete File | unlink |
| Rename Files | rename |
{ "name":"Alice", "gender":"F", "age":11 }
// Use the fs (File System) module to read and
// write to the harddrive
var fs = require('fs');
fs.readFile('kid.txt', (err, data) => {
if (err) {
console.log("Error reading input JSON file.");
}
else {
var p = JSON.parse(data);
console.log("Name: " + p.name);
console.log("Gender: " + p.gender);
console.log("Age: " + p.age);
}
});
Name: Alice Gender: F Age: 11