M
M
Muranx2020-09-03 15:11:12
Node.js
Muranx, 2020-09-03 15:11:12

Is it possible to track the operation of Node.js like debugging native js in the browser?

Hello!

let http = require('http'),
  serv = http.createServer();

serv.on('request', (req, res)=>{
  if(req.url === '/script.js'){
    res.writeHead(200, {'Content-Type' : 'text/javascript'}); // ( 2 )
    res.end(`
        alert('holla from script.js');
      `);
  };
  res.writeHead(200, {'Content-Type' : 'text/html'});
  res.end(`
        <!DOCTYPE html>
        <html lang="en">
        <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>Document</title>
        </head>
        <body>
        <script src="script.js"></script> // ( 1 )
        </body>
        </html>
    `);
}).listen(3000, ()=>{
  console.log('server is already running');
});

Question 1: Where are these documents saved? which I dynamically create using Node.js code, namely this html page, and the script.js script, i.e. I do not physically create them anywhere, I do not read information from the disk, but simply dynamically create a server in which I prescribe the contents of these "files"?
Question 2. It turns out that two requests are triggered in this code (not counting the favicon), is it for html content and for js content of the page?
Question 3. I do not understand this joke when I create a script and attach it to the html code and at the same time under the comment( 2 )I myself prescribe its status in the response header? Those. I tried to write 404, and ofk the script did not connect, why is this header needed? Does it have any practical weight in this example? Why should I dig a hole for myself and indicate, for example, the same 404, when it is easier to always indicate 200?
Thank you for your attention, if you want to throw me a link to the dock, hmm, I took all these methods from the docks, having re-read these methods more than once, I would like a more human answer to questions from your experience!
I forgot the most important thing, is it possible to track the work of node.js somewhere like a native debugger, I’m just damn used to a regular debugger, it’s so convenient, you see when and how it works, right down to the smallest detail, and here node.js everything happens like- as if in the blind (at least while I'm studying)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-09-03
@Muranx

1) are not stored anywhere, the Response object (res.send...) is a stream

The ServerResponse object represents the writable stream back to the client.

https://www.w3schools.com/nodejs/obj_http_serverre...
that is, the bytes are simply sent to the browser in response to a request
2) it is, you can see it in the console, for example
serv.on('request', (req, res)=> { 
  console.log('REQUEST:', req.url)
   .....

3) well, you need to write the status in the response headers, just for granted, if you are sure that you can always give everything from the server without errors, then of course 200 can be everywhere, but
Why should I dig a hole for myself, and indicate, for example, the same 404, when it is easier to always indicate 200?

but if someone rewrites the browser part of the script, and decides to request script2.js, is it better to return a status of 200 or is it still 404?
--
Thank you for your attention, if you want to throw me a link to the dock, hmm, I took all these methods from the docks, having re-read these methods more than once, I would like a more human answer to questions from your experience!

this is an unpopular opinion, but in my opinion it's easier to first start doing something that works on express.js, where the methods of the http module are wrapped in simpler functions, and then, according to circumstances, dig further

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question