basic Flashcards
package that needs to be added to create a nodejs server
const http = require(‘http’);
how do you create a node server once you have declare the correct package (module)
const server = http.createServer(requestListener)
what is a requestListener?
a function that will be executed for each incoming request. You pass it as a parameter to method server.createServer()
what is the structure of this requestListener?
a two parameter arrow function : (req, res) => { }
how does the server work after you set the requestListener function?
it does nothing unless you run server.listen(). This listen method starts a process in which nodes listen to the requests on the port you specified. Example used is server.listen(3000) that will listen on port 3000
what is the core definition of nodejs? what is it fundamentally. some equivalents in other platforms
a runtime. best homologo is aspnet. but any runtime framework (not only web) is
from the terminal point of view what does nodejs keeps doing while it has listeners registered?
he keeps its event loop to listen for requests. this is the core of nodejs. when there’s no listeners registered then there’s no need to be runing and node ends. in terminal you see it the cursor back ready fr you to open word.
main 3 properties of the req object
req.url, req.method, req.headers
famous header that web pages all have? what is its value?
Content-Type that usually is “text/html”
2 most used method in the response object
res.setHeader, res.write
how do we tell the runtime (node) that we are done writing the response?
res.end();
how do you return 200 or 302 or 500
res.statusCode = 302
what 302 means
status code for redirection
how do you write in a file
fs.writeTextSync(‘filename.txt’, “mnmnmn”);
how do you redirect
res.setHeader(‘Location’, ‘/’); res.statusCode = 302