Answer the question
In order to leave comments, you need to log in
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');
});
( 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? Answer the question
In order to leave comments, you need to log in
1) are not stored anywhere, the Response object (res.send...) is a stream
The ServerResponse object represents the writable stream back to the client.
serv.on('request', (req, res)=> {
console.log('REQUEST:', req.url)
.....
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!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question