help icon indicating copy to clipboard operation
help copied to clipboard

Because of browser, node, or my own problem?

Open anlexN opened this issue 9 years ago • 7 comments

  • 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:

default 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

anlexN avatar Oct 13 '17 08:10 anlexN

Chrome is making two HTTP requests, one for /, the other for /favicon.ico. It's just something it does.

bnoordhuis avatar Oct 13 '17 09:10 bnoordhuis

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 avatar Oct 13 '17 11:10 shellberg

@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!

anlexN avatar Oct 13 '17 13:10 anlexN

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

shellberg avatar Oct 13 '17 16:10 shellberg

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 avatar Oct 14 '17 13:10 anlexN

@anlexN Your question has been answered, no? It's the browser.

bnoordhuis avatar Oct 14 '17 15:10 bnoordhuis

Closing as answered, let me know if there is anything outstanding.

gireeshpunathil avatar Apr 02 '18 09:04 gireeshpunathil