Node.js¶
notes¶
- if your request is empty, check if you have body-parser
res.send()
can’t send a number, you should respond with object{ result: number }
post request with fetch¶
fetch("https://cyf-chat-server--ahmadali5.repl.co/messages/newMessage", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(newMsg)
})
Get the full url of the route¶
var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
error anatomy¶
var err = { message: "error message", // accessed by err or err.message
stack: "c:\pth\to\file",
errors: { /* more error data */ }
}
global object¶
global object contains functions, vars and objects we can use without requiring any external module. and it contains:
- console:
console.log(any)
- __dirname: gives us the absolute path to the current directory.
- __filename: gives us the absolute path to the current file.
- require() function: import other modules.
- process object: contains info about the current process.
Require() function¶
- node core modules.
- our own modules.
- third party modules
path module¶
- path.basename(): get the file name only.
path.basename(__filename)
process object¶
process.pid
: get current process id.process.versions.node
: get current node version.- get environment information and vars.
- communicate with the terminal or parent processes through standard input and standard output.
- process.argv: get an array of all arguments passed to this process.
- process.stdout, process.stdin: regulates interactions between terminal and our program.
- exit the current process.
process.exit()