Because of browser, node, or my own problem?
- Node.js Version: 8.7.0
- OS: windows pro x64
- Scope (install, code, runtime, meta, other?): code
- Module (and version) (if relevant): http
This is my source code:
const fs = require('fs'),
http = require('http'),
path = require('path'),
url = require('url')
const hostname = '0.0.0.0',
port = 3000,
server = http.createServer((req, res) => {
console.log(typeof req.url)
console.log(req.url)
let contenttype,
extname,
filepath = '.' + req.url,
mimetypes = {
'.css': 'text/css',
'.eot': 'application/vnd.ms-fontobject',
'.gif': 'image/gif',
'.html': 'text/html',
'.jpg': 'image/jpg',
'.js': 'text/javascript',
'.json': 'application/json',
'.mp4': 'video/mp4',
'.otf': 'application/font-otf',
'.png': 'image/png',
'.svg': 'application/image/svg+xml',
'.wav': 'audio/wav',
'.woff': 'application/font-woff'
}
if (filepath == './') {
filepath = './index.html'
}
extname = String(path.extname(filepath)).toLowerCase(),
contenttype = mimetypes[extname] || 'application/octet-stream'
fs.readFile(filepath, (err, data) => {
if (err) {
if (err.code == 'ENOENT') {
fs.readFile('./404.html', (err, data) => {
res.writeHead(200, { 'Content-Type': contenttype })
res.end(data, 'utf-8')
})
} else {
res.writeHead(500)
res.end(`Sorry, check with the site admin for error: ${error.code}`)
}
} else {
res.writeHead(200, { 'Content-Type': contenttype })
res.end(data, 'utf-8')
}
})
})
server.listen(port, hostname)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>
hello anlex
</h1>
</body>
</html>
node console screenshot:
I review node ducument req.url, know req.url return string, not other type!
when i access http://192.168.199.170:3000
But just like my node console screenshot, give me amazing problem! why? why chrome give me two answer, because load index.js twice? @mikeal , @indexzero @snostorm , @pmq20 , @lance , and other good man
Chrome is making two HTTP requests, one for /, the other for /favicon.ico. It's just something it does.
And, not just Chrome, but pretty much all modern browsers (Firefox, Opera, IE, Safari, ...) that support a tabbed interface (based on a Tabbed Document Interface).
@shellberg , the fact is not your said.
I just see chrome, opera, and any other browser based on chromium do it; but microsoft edge, firefox don't!
Firefox originally went the way of implementing <link re="icon"> concerning the favicon feature ; my poor memory (but it was fun revisiting an old thread!) ; there was plenty of discussion around at the time of how to respond to the non-standard IE 'feature'.
However, Firefox is definitely capable of the favicon feature, but its controlled by preferences; looks like the preferences that might be dominant currently are... browser.chrome.favicons (default: true) and browser.chrome.site_icons (default: true)
( You can check what are in effect for you with about:config in your Firefox. )
And, running your code once against my current Firefox browser (with the default preferences above) :
# node favicon.js
string
/
string
/favicon.ico
string
/favicon.ico
But, then once the response is cached, it thereafter returns
# node favicon.js
string
/
YMMV
I think you don't understand my word. I mean that google chrome and browser based on chromium, why always return /favicon.ico ! @shellberg , why do you always see firefox?
@anlexN Your question has been answered, no? It's the browser.
Closing as answered, let me know if there is anything outstanding.