简体   繁体   English

为什么我应该在带有 node.js 的 http 服务器中的请求处理对象上声明 handle["/"] ?

[英]why should i declare handle["/"] at request handling object in http server with node.js?

I have encountered with this nice tutorial to node.js .我遇到过这个很好的node.js 教程 There is some example there :那里有一些例子:

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);

Why do I need handle["/"] what is it good for?为什么我需要handle["/"]它有什么用?

he says there :他在那里说:

As you can see, it's really simple to map different URLs to the same request handler: by adding a key/value pair of "/" and requestHandlers.start, we can express in a nice and clean way that not only requests to /start, but also requests to / shall be handled by the start handler.如您所见,将不同的 URL 映射到同一个请求处理程序非常简单:通过添加“/”和 requestHandlers.start 的键/值对,我们可以用一种漂亮而干净的方式表达,不仅请求 /start ,但对 / 的请求也应由启动处理程序处理。

why that solve any problem and what is the problem at all?为什么这可以解决任何问题,问题到底是什么?

The function binded to handle["/"] will be called when somebody visits yoursite.com, and handle["/start"] will be called for visits to yoursite.com/start当有人访问yoursite.com时会调用handle["/"]绑定的函数,访问yoursite.com/start时会调用handle["/start"]

The author is trying to build a server that will display different things based on routes (urls).作者正在尝试构建一个服务器,该服务器将根据路由(url)显示不同的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM