无法阅读财产' /'未定义的

时间:2016-12-21 10:11:39

标签: javascript node.js

我开始研究node.js.我在学习的时候问你问题。当我运行我的代码(服务器)并连接到localhost时,它无法正常工作。

这是错误: enter image description here

这是我的代码:

index.js

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

var handle = {};
handle['/'] = requestHandlers.view;
handle['/view'] = requestHandlers.view;
handle['/create'] = requestHandlers.create;

server.start(router.route, requestHandlers.handle);

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('\nrequest for ' + pathname + ' received.');

      response.writeHead(200, {'Content-Type' : 'text/plain'});
      // route(handle, pathname); // injected function call
      var content = route(handle, pathname);

      response.write(content);
      response.end();
    }

  http.createServer(onRequest).listen(8000);

  console.log('server has started.');
}

exports.start = start;

router.js

function route(handle, pathname) {
    console.log('about to route a request for ' + pathname);
    if (typeof handle[pathname] === 'function') {
        return handle[pathname]();
    } else {
        console.log('no request handler found for ' + pathname);
        return "404 Not found";
    }
}

exports.route = route;

requestHandlers.js

function view(response) {
    console.log('request handler called --> view');
    return "Hello View";
}

function create(response) {
    console.log('request handler called --> create');
    return "Hello Create";
}

exports.view = view;
exports.create = create;

1 个答案:

答案 0 :(得分:4)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="col-md-6 col-sm-6"> <div class="block-image-round margin-top-120"> <img src="http://placehold.it/200/300" style="width:100%; " alt="" data-toggle="modal" data-target="#myModal"> </div> </div> <div id="myModal" class="modal fade" role="dialog" style="background-color: white"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title">Modal Header</h4> </div> <div class="modal-body"> <p>Some text in the modal.</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div>中,您传递的是不存在的index.js,而不是您创建的requestHandlers.handle对象。

handle