节点入门书 – 请求处理程序
正如标题所说,我有什么node.js命令提示说谎在requestHandlers.js文件中的问题。 我遵循Node Beginner Book中的指南,直到现在,这本书没有任何问题 – 或者说我的代码。
我有以下input:
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);
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); response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); } http.createServer(onRequest).listen(8888); 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') { handle[pathname](); } else { console.log("No request handler found for " + pathname); } } exports.route = route;
requestHandlers.js
function start() { console.log("Request handler "start" was called."); } function upload() { console.log("Request handler "upload" was called."); } exports.start = start; exports.upload = upload;
我有这个输出:
“C:\ Program Files(x86)\ nodejs \ requestHandlers.js:1 console.log(”Request handler“start”was
SyntaxError: Unexpected identifier at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Module.require (module.js:364:17) at require (module.js:380:17) at Object.<anonymous> (C:\Program Files (x86)\nodejs\index.js:1:153) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32)"
我真的不知道问题出在哪里。 我遵循了书中的指南,我要么从书上复制粘贴代码,要么自己写代码。 我已经doublechecked代码的错误,但还没有find任何。 我记得例如使用“函数”,所以在node.js中input代码时不会有任何错误。
所以任何帮助将不胜感激!
谢谢。
您正在JavaScript中混合不同的string标记。
function start() { console.log("Request handler "start" was called."); } function upload() { console.log("Request handler "upload" was called."); }
你可以使用“开始和结束的string,但像"Request handler "start" was called."
是无效的,因为你终止string在“开始,并开始一个新的string” 被调用 。
如果start
和upload
是variables,只需将string连接成如下所示:
"Request handler " + start + " was called."
或者把你的string改成这样:
"Request handler 'start' was called."
或删除“周围的开始和上传。