I wrote a simple HTTP sever that serves some HTML.
Here’s the code :
var http = require('http');
http.createServer(function(req, res) {
res.writeHeader(200, {"Content-Type": "text/html"});
var html = '<DOCTYPE!>' +
' <html>' +
' <head>' +
' <title>' +
' Test page' +
' </title>' +
' </head>' +
' <body>' +
' <p>' +
' This is a test page !' +
' </p>' +
' </body>' +
' </html>';
res.write(html);
res.end();
}).listen(8080);
The resulting HTML page is the following :
<html>
<head>
</head>
<body>
<doctype!>
<title> Test page </title>
<p> This is a test page ! </p>
</doctype!>
</body>
</html>
So my questions are the following :
- Why is the html “string” included in the body of the HTML ?
- Is it possible to have indentation in the HTML besides using a template engine (jade) ?
And the last question is a bit separate :
- If I have a page called
index.htmlwhich only showsimg.jpg. How can I know that a users request toimg.jpgis related toindex.html?
What I mean by “related” is : “thatimg.jpgis a linked / a dependency ofindex.html“.
Thank you in advance !
That’s because
<DOCTYPE!>isn’t valid. It should be:As is, it appears more like an element to the browser, which attempts to normalize the markup and places it into the
<body>.Many template engines, including Jade, actually minify the markup, removing all unnecessary whitespace for smaller bandwidth consumption.
But, you could look at using a “beautifier,” such as html.
You can view “Resources” in most in-browser debuggers, which will list images, scripts, stylesheets, etc. that your page has requested.
You could also try web scraping your application, perhaps with jsdom: