basic Flashcards
(39 cards)
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
how do you create the package.json
npm init and filling the information.
what is the name of the field in the package json where you add the autorun configuration? y dentro de este? y then how you specify the app to run?
“scripts” y dentro de este “start”: “node app.js”
how do you run your code from the autorun configuration?
npm start
install nodemon in prod
npm install nodemon –save
install nodemon in dev
npm install nodemon –save-dev
install nodemon globally
npm install nodemon -g
what is the name of folder created by nodejs when you add nodemon
node_modules
when you install nodemon what is added to package. json? y si fuera installed solo para production?
“dev-dependencies”: {
“nodemod”: “^1.2.3”
}
“dependencies” : {
how to make a modules update?
npm install