Node.js Flashcards
What makes Node.js good as a runtime environment?
It is non-blocking, which means it can handle concurrent connections efficiently
What does GET do, is it safe and can it be cached?
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
What does POST do? Is it safe and is it cached?
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
What does PUT do?
Used to update a resource on the server, it may require additional security considerations.
What does PUT being idempotent entail?
Multiple identical requests will produce the same result as a single one
Write
How to import the http module?
const http = require(“http”);
Write
How to create a server?
const server = http.createServer();
server.listen(port);
server.listen(3000, () => {console.log(…);}); to check functionality
Write
The configuration of a createServer()
const server = http.createServer((req, res) => {console.log(“res”)});
Write
How to send response from server?
res.write(“Response”);
Write
How to finish response?
res.end();
How to send Header in server?
res.writeHead(200, {“Content type” : “text/plain”});
What pieces can be parsed from the following URL:
http://site.com/path/?q=val#hash
In order:
site.com hostname
/path/ pathname
q=val query
#hash hash
Write
How to implement routes for different HTTP methods? Write some statements accordingly
We use the req.method
if (req.method===”GET”){
res.writeHead(200, {“Content type” : “text/plain”});
res.end(“GET method used”);
}
Write
The two required statements to parse request data
const url = require(“url”);
const querystring = require(“querystring”);
Write
Given the required modules, how to parse a URL?
const parsedURL = url.parse(req.url);
const queryParams = querystring.parse(parsedURL.query);