我们接着改造服务器,让请求处理程序能够返回一些有意义的信息。
我们来看看如何实现它:
1、让请求处理程序通过onRequest函数直接返回(return())他们要展示给用户的信息。
2、让我们从让请求处理程序返回需要在浏览器中显示的信息开始。
我们需要将requestHandler.js修改为如下形式:
复制代码 代码如下:
function start() {
console.log("Request handler 'start' was called.");
return "Hello Start";
}
function upload() {
console.log("Request handler 'upload' was called.");
return "Hello Upload";
}
exports.start = start;
exports.upload = upload;
同样的,请求路由需要将请求处理程序返回给它的信息返回给服务器。
因此,我们需要将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;
正如上述代码所示,当请求无法路由的时候,我们也返回了一些相关的错误信息。
最后,我们需要对我们的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.");
response.writeHead(200, {"Content-Type": "text/plain"});
var content = route(handle, pathname);
response.write(content);
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start=start;
如果我们运行重构后的应用:
请求http://localhost:8888/start,浏览器会输出“Hello Start”,
请求http://localhost:8888/upload会输出“Hello Upload”,
而请求http://localhost:8888/foo 会输出“404 Not found”。
这感觉不错,下一节我们要来了解一个概念:阻塞操作。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 陈秋霞.1977-温馨在我心(LP版)【歌林】【WAV+CUE】
- 张秀卿.2019-感情骗子【豪记】【WAV+CUE】
- 林隆璇.1993-收藏昨日【福茂】【WAV+CUE】
- 魔兽世界wlk德拉诺金钻如何获得 wlk德拉诺金钻获取方法介绍
- 魔兽世界wlk祖阿曼战熊如何获得 wlk祖阿曼战熊获取方式介绍
- 魔兽世界阿奴巴拉克有什么背景故事 阿奴巴拉克背景故事介绍
- NeilYoung-EarlyDaze(2024)[24Bit]WAV
- NeaveTrio-Rooted(2024)[24Bit-WAV]
- [吉他演奏]RCAREDSEAL《Rodrigo-ConciertoDeAranjues》[WAV+CUE]
- 气质绝了!《黑神话》蜘蛛四妹演员COS自己正片释出
- 《黑神话:悟空》第五章动画大闹天宫内容被删:牛魔王是主角
- 玩家吐槽《黑神话》如意金箍棒属性差:就该是最强武器
- 《邓紫棋 上古情歌2CD》[WAV/分轨][1.1GB]
- 崔健《摇滚交响音乐会 2CD》[WAV/分轨][730MB]
- 群星《顶级发烧大碟 蒙面歌王争霸战 2CD》[WAV/分轨][1.3GB]