Node.js Flashcards

1
Q

What makes Node.js good as a runtime environment?

A

It is non-blocking, which means it can handle concurrent connections efficiently

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does GET do, is it safe and can it be cached?

A

GET retrieves data from the server, it is safe as it does not have any side effects on the server and it can be cached

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does POST do? Is it safe and is it cached?

A

Used to submit data to be processed by the server, it is not safe as it may have side effects of the server and they are not cached

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does PUT do?

A

Used to update a resource on the server, it may require additional security considerations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does PUT being idempotent entail?

A

Multiple identical requests will produce the same result as a single one

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write

How to import the http module?

A

const http = require(“http”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write

How to create a server?

A

const server = http.createServer();
server.listen(port);

server.listen(3000, () => {console.log(…);}); to check functionality

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write

The configuration of a createServer()

A

const server = http.createServer((req, res) => {console.log(“res”)});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write

How to send response from server?

A

res.write(“Response”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write

How to finish response?

A

res.end();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to send Header in server?

A

res.writeHead(200, {“Content type” : “text/plain”});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What pieces can be parsed from the following URL:
http://site.com/path/?q=val#hash

A

In order:
site.com hostname
/path/ pathname
q=val query
#hash hash

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write

How to implement routes for different HTTP methods? Write some statements accordingly

A

We use the req.method
if (req.method===”GET”){
res.writeHead(200, {“Content type” : “text/plain”});
res.end(“GET method used”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write

The two required statements to parse request data

A

const url = require(“url”);
const querystring = require(“querystring”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write

Given the required modules, how to parse a URL?

A

const parsedURL = url.parse(req.url);
const queryParams = querystring.parse(parsedURL.query);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Write

How to import express.js

A

const express = require(“express”);
const app = express();

17
Q

Write

How to handle routes with express

A

const express = require(“express”);
const app = express();

app.post(“action”, (req, res) => {res.send(“…”);});

18
Q

Write

How to file I/O?

A

const fs = require(“fs”);
fs.writeFile(“fileToWriteTo.txt”, “Written Text”);
fs.readFile(“fileToReadFrom.txt”);

19
Q

What does path.join() do?

What module does it require?

A

It constructs a valid file path, from various parts of file paths.

const path = require(“path”), also requires express

20
Q

What is __dirname?

A

It represents the directory of the current module, where the script is located

21
Q

What does express.static? What does it do?

A

It is middleware employed to serve static files. It also makes files in the “public” directory static resources, meaning they are available to clients.

22
Q

Write

The statements required for static file handling (the required modules and the code)

A

const express = require(“express”);
const app = express();
const path = require(“path”);

app.use(express.static(path.join(__dirname, “public”)));

23
Q

What are cookies (simplest definition)?

A

Small pieces of data stored on the client’s browser.

24
Q

What is the purpose of cookies?

A

They store information that is accessible across different pages/sessions (user preferences, tracking user behavior, etc…)

25
Q

Write

How to implement the cookieParser?

A

const express = require(“express”);
const app = “express”
const cookieParser = require(“cookie-parser”);

app.use(cookieParser());

26
Q

Write

How to set a cookie?

A

app.get(“/set-cookie”, (req, res) => {
res.cookie(“username”, “Lud”);
res.end(“Cookie is set”);});

27
Q

Write

How to read a cookie?

A

req.cookies.property

28
Q

What are sessions?

A

A mechanism for maintaining user state across multiple requests, they are stored on the server

29
Q

Write

How to use express-session middleware?

A

const express = require(“express”);
const session = require(“express-session”);
const app = express();

app.use(session({
secret: “your-secret-key”;}));

30
Q

Write

How to set a session?

A

app.get(“/set-session”, (req, res) => {
req.session.username(“Dave”);
res.send(“username set”);});

31
Q

Write

How to get a session?

A

app.get(“/get-session”, (req, res) => { const username = req.session.username;
res.send(“Hello, ${username}”);});