Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs throwing TypeError('first argument must be a string, Array, or Buffer');

Tags:

node.js

I am following a nodejs book which is online: http://nodebeginner.org/ and stuck at one of the section. In that section (http://nodebeginner.org/#head22), it requires me to create the following 4 files:

**index.js**:

var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");

var handle = {};
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;

server.start(router.route, handle);



**requestHandlers.js**:

function start(){
        console.log("Request handler 'start' was called.");
        return "Hello start";
}


    function upload(){
            console.log("Request handler 'upload' was called.");
            return "Hello Upload";
    }

    exports.start = start;
    exports.upload = upload;


**router.js**:
function route(handle, pathname){
        console.log("About to route a request for " + pathname);
        if(typeof handle[pathname] === 'function'){
                handle[pathname]();
        }else{
                console.log("No request handler found for " + pathname);
                return "404 Not found";
        }
}

exports.route = route;

**server.js**:

var http = require("http");
var url = require("url");

function start(route, handle){
        function onRequest(request, response){
                var pathname = url.parse(request.url).pathname;
                console.log("Request for " + pathname + " received.");

                response.writeHead(200, {"Content-Type":"text/plain"});
                var content = route(handle, pathname);
                response.write(content);
                response.end();
        }

        http.createServer(onRequest).listen(8888);
        console.log("Server has started.");
}

exports.start = start;

When I run, it returns me the following error:

Server has started. Request for / received. About to route a request for / Request handler 'start' was called.

http2.js:598 throw new TypeError('first argument must be a string, Array, or Buffer'); ^ TypeError: first argument must be a string, Array, or Buffer at ServerResponse.write (http2.js:598:11) at Server.onRequest (/var/www/node/server.js:11:12) at Server.emit (events.js:70:17) at HTTPParser.onIncoming (http2.js:1451:12) at HTTPParser.onHeadersComplete (http2.js:108:31) at Socket.ondata (http2.js:1347:22) at TCP.onread (net_uv.js:309:27)

I could trace the error to server.js and when I commented out these 2 lines, it works:

    var content = route(handle, pathname);
    response.write(content);

Where am I doing it wrong?

like image 277
Scott Avatar asked Dec 21 '22 08:12

Scott


1 Answers

You're forgetting to return the value on the 4th line of router.js

    handle[pathname]();

It will work properly if you change it to:

    return handle[pathname]();
like image 112
fourk Avatar answered Dec 24 '22 02:12

fourk