0

node.js で作業すると、このエラーが私を悩ませています。ここでエクスプレスで何かが起こっていると思います: var app = express(); http.createServer(app)?? 何かアドバイス?

Listening to port 8000
Request / from 127.0.0.1 to localhost:8000

http.js:686
throw new Error('Can\'t render headers after they are sent to the client.'
      ^
Error: Can't render headers after they are sent to the client.

ここにコード:

var sys = require('sys'), 
http = require('http');

var express = require('express'),
    routes = require('./routes');

var app = express();
var _self = this;


var _routes = {
    '/' : function(request, response) {
        response.writeHead(200, {'Content-Type': 'text/html'});
        response.write('hello world\n');
        response.end();
    },

    '/is_up' : function(request, response) {
        response.writeHead(200, {'Content-Type': 'text/plain'});
        response.write('ok');
        response.end();
    },
};

var _requestHandler = function(request, response) {
    console.log('Request '+request.url+' from '+request.connection.remoteAddress+' to '+request.headers.host);
    if(_routes[request.url] === undefined) {
        response.writeHead(404, {'Content-Type': 'text/plain'});
        response.write('not found\n');
        //response.end();
    } else {
        _routes[request.url].call(this, request, response);
    }
};

var _server = module.exports = http.createServer(app).
                    addListener('request', _requestHandler)
                    .listen(8000);
    console.log('Listening to port ' + "8000");
4

1 に答える 1