gpt4 book ai didi

node.js - Node 初学者书籍 - requestHandlers

转载 作者:可可西里 更新时间:2023-11-01 16:33:37 28 4
gpt4 key购买 nike

正如标题所说,我对 node.js 命令提示符在 requestHandlers.js 文件中的内容有疑问。我正在按照 Node 初学者手册中的指南进行操作,直到现在,这本书或我的代码都没有出现任何问题。

我有以下输入:

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

服务器.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;

路由器.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;

请求处理程序.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:1console.log("请求处理程序"开始"是

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

我真的看不出问题出在哪里。我已经按照书中的指南进行操作,我要么从书中复制粘贴代码,要么自己编写。我已经仔细检查了代码是否有错误,但没有发现任何错误。例如,我记得使用了\'function\',这样在node.js中输入时代码不会有任何错误。

因此,我们将不胜感激!

谢谢。

最佳答案

您在 JavaScript 中混合使用不同的字符串标记。

function start() {
console.log("Request handler "start" was called.");
}
function upload() {
console.log("Request handler "upload" was called.");
}

您可以使用 "作为字符串的开头和结尾,但是类似 "Request handler "start"was called."

无效,因为您在 "start 处终止字符串并在 "was called 处开始一个新字符串。

如果 startupload 是变量,只需将字符串与这样的内容连接起来:

"Request handler " + start + " was called."

或者把你的字符串改成这样:

"Request handler 'start' was called."

或删除围绕开始和上传的 "。

关于node.js - Node 初学者书籍 - requestHandlers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25103036/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com