简体   繁体   中英

Function executed, but throwing error

Trying to write the following code: index.js

const http = require('http');
const port = 9090;
const url = require('url');
const handlers = require('./handlers.js');


http.createServer((req, res) => {
    req.path = url.parse(req.url).pathname;
    let handler = handlers(req.path);
    handler(req, res);
}).listen(port);

handlers.js:

const fs = require('fs');
const homeHandler = require('./handlers/home-handler');
const contentHandler = require('./handlers/content-handler');

module.exports = (path) => {
    switch (path) {
        case '/':
            return homeHandler;
        case '/content':
            return contentHandler;
    }
}

home-handler.js

const fs = require('fs')

module.exports = (req, res) => {
    fs.readFile('./index.html', (err, data) => {
        if (err) {
            console.log(err.message);
            return;
        }

        res.writeHead(200, {
            'content-type': 'text/html'
        });
        res.write(data);
        res.end();
        return;
    });
};

When I "Launch program" and run in browser localhost:9090 , the function handler is executed in browser, but in the debug console it throws:

TypeError: handler is not a function

Using console.log(handler) it shows that handler is function, also instanceof shows that handler is instance of Function. What's wrong with this?

Your handlers function only handle two specific inputs, / and /content . Any other request will produce an error, since handlers will not return a function.

You might say, "That's fine! I'm not requesting a path outside of that set of two paths," but I think you might be wrong.

Most browsers will make a request for a favicon by requesting /favicon.ico in addition to making a request for the actual URL you typed in. If the browser does this additional favicon request, you will see successful results for you intended request (as you do), but then also see a failure message for the additional favicon request, which you haven't set up your code to handle.

I suggest adding a default debug handler:

module.exports = (path) => {
    switch (path) {
        case '/':
            return homeHandler;
        case '/content':
            return contentHandler;
        default:
            (req, res) => {
                res.end("Path not handled");
                console.warn("No handler for", req.url);
            }
    }
}
const fs = require('fs')
const faviconIco = '/favicon.ico'
module.exports = (req, res) => {
    fs.readFile('.' + faviconIco, (err, data) => {
        if (err) {
            console.log(err.message)
            return
        }

        res.writeHead(200, {
            'content-type': 'image/x-icon'
        })
        res.write(data)
        res.end()
    })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM