Intro And NPM Flashcards
Two things to do to run a server ?
- Create a server
- Start a server
Http.createServer accepts what ?
A call back function with req and response options
What is the standard IP address of the local host ?
127.0.0.1
How can we see what options req method has ?
We can console log req parameter when we are creating a server. And we first create a server which takes a call back function this is where we can send back a response. And then we run the server by running listen method and this takes a couple of parameters like port number, host address and a call back function which you can use to console log server is running
What module do we use in default node to set up routing ?
We use url module
How do we implement the routing using http module ?
We save the req.url to a variable and then use a big if else statement to define routes for different requests
Where do we assign a variable to req.url and where do we use if else statements to implement routing ?
If the the create server call back function
How do you set a http status code ? Like 404
In the pure NodeJS example, you can use res.writeHead() method to send back a response. It will look like this
res.writeHead(404)
How do you set content type in the header ?
You can send it with res.writeaHead(404, {
‘content-type’:’text/html/})
Content type is written as the second parameter in writeHead()
How do you write Jason code ?
Both key value pairs should be strings separated by : inside curly bases. Json looks a lot like objects
Give an example of receiving data in a Json file and then reading it. In this case console log it.
fs.readFile(‘./dev/text.txt’, ‘utf-8’, (err, data) =>{
const productData = JSON.parsedata(data)
console.log(productData)})
How do you know which code will be executed once only ?
All the top level code gets executed once only
When to use the sync version ? In this case give an example of fs.readFileSync
Whenever the code is supposed to be used once is when we can use sync version because sync version only gets executed once and same goes for the top level code.
Give an example of adding place holders in HTML file
<h1>{%PRODUCTNAME%}<h1></h1>
</h1>
When does readFileSync return a buffer ?
It returns a buffer when you don’t specify an encoding