- How do you create a Node project, complete with a package.json file?
Answer: in your Command Prompt/Terminal Window, navigate to inside your project folder,
for example, tutorial2c-smith. Then type npm init and answer the prompts. Hit return
for the defaults.
- How do you install a module, such as http-status-codes, in a Node project?
How do you use this module in the main.js file?
Answer: in the Command Prompt/Terminal Window, enter this command:
npm i http-status-nodes
The i means install. Then include this link at the top of your main.js script:
const HttpStatusCodes = require("http-status-codes");
Note that variable names in JavaScript can't contain dashes, so HttpStatusCodes is used instead of http-status-codes.
- How do you write "The <br> tag" in a paragraph
within a HTML document?
Answer: <p>The <br> tag.</p>
- In a Node server (see the SimpleServer Example), when you are
hardcoding browser content, how do you specify the type of the content in the HTML header?
See the HTTP Headers document for commonly used content types.
Answer: You specify the type of content in the HTTP header using
response.writeHead:
response.writeHead(200, {"Content-type":"text/html"});
or
response.writeHead(HttpStatusCodes.StatusCodes.OK,
{"Content-type":"text/html"});
- German Numbers Example. Start with this array of German numbers:
var numbers = ["eins", "zwei", "drei", "vier", "fünf"];
Modify the SimpleServer Example to output
this array as an ordered list to the browser:
- eins
- zwei
- drei
- vier
- fünf
Use a modern for loop to serve the contents
of the array. Answer:
// Source code file: main.js
const PORT = 3000;
const http = require("http");
const app = http.createServer((req, res) => {
console.log("Received an incoming request.");
res.writeHead(200, {"Content-Type": "text/html" });
var numbers = ["eins", "zwei", "drei", "vier", "fünf"];
res.write("<head>");
res.write('<meta charset="UTF-8">');
res.write("</head>");
res.write("<h1>German Numbers</h1>");
res.write("<ol>");
for(var n of numbers) {
res.write(`<li>${n}</li>`);
}
res.write("</ol>");
res.end( );
console.log("Response sent.");
});
app.listen(PORT);
console.log(`Server started; is listening on port ${PORT}`);
- TypewriterBox Example. Modify the SimpleServer Example to display a box like this:
+----------+
| |
| |
| |
| |
+----------+
Enclose the box in <pre> tags;
define the height of the box like this:
const HEIGHT = 5;
<pre> tags define preformatted text, where text is displayed using a
fixed-width font with spaces and line breaks preserved. Answer:
// Source code file: main.js
var http = require("http");
const app = http.createServer((req, res) => {
console.log("Received an incoming request.");
res.writeHead(200, {"Content-Type": "text/html" });
res.write("<h1>Typewriter Graphics Box</h1>");
const HEIGHT = 5;
res.write("<pre>\n");
res.write("+----------+\n");
for(var i = 1; i <= HEIGHT; i++) {
res.write("| |\n");
}
res.write("+----------+\n");
res.write("</pre>");
res.end( );
console.log("Response sent.");
});
app.listen(3000);
console.log("Server started; is listening on port 3000");
- What are the response methods that are used
to write to the localhost browser?
Ans: writeHead write end
- How can you get the following statements to work?
console.log("This is
a test.");
Answer: you can either use the \n entity like this:
console.log("This is\na test.");
or use backquotes like this:
console.log(`This is
a test.`);
- JSON means JavaScript Object Notation. It is popular for representing data
for a wide variety of programming languages.
- JSON has replaced XML because it is simpler and more compact.
- XML was very popular from the early 1990s until about 2012, when JSON
replaced XML in popularity.
- JSON can represent arrays, object literals, arrays of object literals, and
object literals nested inside of object literals.
- JSON always uses double quotes instead of single quotes or no quotes for strings.
- Use the JSON.stringify method to convert an array of object
literals into JSON notation. For example:
var kids = [{name: 'Alice', gender: 'F', age: 11},
{name: 'Bob', gender: 'M', age: 10},
{name: 'Pat', gender: 'X', age: 16}];
var kidsJson = JSON.stringify(kids);
console.log(kidsJson);
// Output:
[{"name":"Alice","gender":"F","age":11},{"name":"Bob","gender":"M","age":10},
{"name":"Pat","gender":"X","age":16}]
- Use the JSON.parse method to change the JSON string back
into an array of object literals:
var kids2 = JSON.parse(kidsJson);
console.log(kids2);
// Output:
[
{ name: 'Alice', gender: 'F', age: 11 },
{ name: 'Bob', gender: 'M', age: 10 },
{ name: 'Pat', gender: 'X', age: 16 }
]