轻松创建nodejs服务器(6):作出响应
作者:bea
我们接着改造服务器,让请求处理程序能够返回一些有意义的信息。 我们来看看如何实现它: 1、让请求处理程序通过onRequest函数直接返回(return())他们要展示给用户的信息。 2、让我们从让请求处理程序返回需要在浏览器中显示的信息开始。 我们需要将requestHandler.js修改为如下形式: 代码如下: function start() { console.log("Request handler 'start' was called."); re
我们接着改造服务器,让请求处理程序能够返回一些有意义的信息。
我们来看看如何实现它:
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”。
这感觉不错,下一节我们要来了解一个概念:阻塞操作。
有用 | 无用
我们来看看如何实现它:
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”。
这感觉不错,下一节我们要来了解一个概念:阻塞操作。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- 简单谈谈jQuery(function(){})与(function(){})(jQuery)
- jQuery多级弹出菜单插件ZoneMenu
- 使用jquery菜单插件HoverTree仿京东无限级菜单
- jQuery实现视频作为全屏幕背景
- JS实现判断滚动条滚到页面底部并执行事件的方法
- 轻松创建nodejs服务器(10):处理上传图片
- 轻松创建nodejs服务器(10):处理POST请求
- 浅析jQuery EasyUI中的tree使用指南
- 轻松创建nodejs服务器(7):阻塞操作的实现
- JavaScript实现弹出子窗口并传值给父窗口
- JavaScript获取Url里的参数
- 轻松创建nodejs服务器(8):非阻塞是如何实现的
- 轻松创建nodejs服务器(9):实现非阻塞操作
- Javascript实现单张图片浏览
- VS2008中使用JavaScript调用WebServices
- JavaScript中window.showModalDialog()用法详解
- JavaScript获取网页、浏览器、屏幕高度和宽度汇总
- Js使用WScript.Shell对象执行.bat文件和cmd命令
- 使用jQuery jqPlot插件绘制柱状图