Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6609103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:40:49+00:00 2026-05-25T19:40:49+00:00

I am following a nodejs book which is online: http://nodebeginner.org/ and stuck at one

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:40:49+00:00Added an answer on May 25, 2026 at 7:40 pm

    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]();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading through the excellent online book http://nodebeginner.org/ and trying out the simple code
At http://nodejs.org/#about it says the following: Node is similar in design to and influenced
I've installed nodeJS ( from http://nodejs.org/ ). This has installed nodeJS & npm. Then
I have an NodeJS Express app that is getting really big in just one
I downloaded the nodeJs package installer for OSX http://nodejs.org/dist/v0.6.5/node-v0.6.5.pkg But when I run node
Checking out the following modules for connecting to mysql db: https://github.com/sidorares/nodejs-mysql-native http://github.com/felixge/node-mysql http://github.com/Sannis/node-mysql-libmysqlclient Do
I have an XML document which contains nodes like following:- <a class=custom>test</a> <a class=xyz></a>
I have the following problem with Tikz/Latex: I have some nodes that contain text.
I'm beginning under NodeJS/Express and I'm facing the following problem (I probably didn't get
I am using nodejs. And calling something like the following from a javascript file:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.