简体   繁体   中英

TypeError: Cannot call method 'writeHead' of undefined

I am running a simple nodejs application using index, server, router and requestHandler and it shows an error as soon as the line response.writeHead(200,{"Content-Type":"text/html"}); is reached. The code is:

File requestHandler.js:

    var querystring=require("querystring"),
    fs=require("fs"),
    formidable=require("formidable");

function start(response){
    console.log("Request handler 'start' was called.");
    var body='<html>'+
        '<head>'+
        '<meta http-equiv="Content-Type"'+
        'content="text/html; charset=UTF-8"/>'+
        '</head>'+
        '<body>'+
        '<form action="/upload" enctype="multipart/form-data"'+
        'method="post">'+
        '<input type="file" name="upload" mulitple="multiple">'+
        '<input type="submit" value="Upload file"/>'+
        '</form>'+
        '</body>'+
        '</html>';
    response.writeHead(200,{"Content-Type":"text/html"});
    response.write(body);
    response.end();
}

File index.js:

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

var handle={}
handle["/"]=requestHandlers.start;
handle["/start"]=requestHandlers.start;
server.start(router.route, handle);

File router.js:

function route(handle, pathname, request, response){
    console.log("About to route request for "+pathname);
    if (typeof handle[pathname] === 'function'){
        handle[pathname](request, response);
    }else{
        console.log("No request handler found for" +pathname);
        response.writeHead(404, {"Content-Type":"text/html"});
        response.write("404 Not found");
        response.end();
    }
}
exports.route=route;

File 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");
            route(handle, pathname);
            route(handle,pathname,response,request);
        }
        http.createServer(onRequest).listen(8888);
        console.log("Server has started");
    }
exports.start=start;

A few errors in your code that I would like to point out-

The RequestHandlers function start is not being exported. Hence, it is impossible for index.js to use that function. You need to add this firstly in your requestHandlers.js file

exports.start = start;

The next issue is in router.js. If the if condition is satisfied, you are calling the function pointed by the variable handle[pathname]. But you are passing two parameters- request and response. You do not need to pass the start function with request because- a) That function does not accept that as a parameter and b) You are not looking to gather anything for the request. If you do need to take some information from the request(a variable passed in the URL), you could add that parameter to start function and deal with it appropriately. But for now just remove that parameter when you call handle[pathname]. So, edit this line in router.js-

handle[pathname](response);

Finally, one very small problem in server.js. You are not passing the correct number of parameters in the first call to route. So remove route(handle,pathname). Secondly, in your second call, you swapped the two variables. You cannot do that with request and response because node.js looks for the variable reponse for the response to be outputted and request is for any requests made to the server. You need to correct the call made to server.js

route(handle,pathname,request,response);

This should solve it for you. I hope this helped. Let me know if there is anything else I can explain.

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