Node.JS中http路由服务实现是怎样,要点有哪些
Admin 2022-08-18 群英技术资讯 1315 次浏览
今天小编跟大家讲解下有关“Node.JS中http路由服务实现是怎样,要点有哪些”的内容 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了相关资料,希望小伙伴们看了有所帮助。开始学习nodejs!
参考书籍:The Node Beginner Book ,所有问题和讨论都围绕本书。
1.学习nodejs需要具备的基础知识:
js基本语法,基本上写过前端的都能满足,原生js、jquery
2.nodejs与基础知识相比,学习的点在哪里?
nodejs本身就是js,如下:
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
nodejs使用模块化编程
关于模块化,只要知道它解决什么问题即可:
模块化其实就是封装,把一组特定功能的代码封装在一个类似java jar包的模块中,方便复用,使结构清晰,等等
js从原生的顺序无模块代码,到自执行函数、jquery匿名函数、到CommonJS,就是一个模块化进程,习惯使用jquery的童鞋不应该抵触新的模块化编程
nodejs的模块化,我们都要学习哪些?
1.使用现成模块工具
如上代码,copy到server.js中,cmd命令下执行 node server.js
访问 http://localhost:8888/ ,一个简单的web服务就成功访问了!
这里使用 require("http") 调用http模块,这个是nodejs内置的
2.自己封装模块
server.js可能我们会加入更多其他的路由代码,这时候我们可以把启动功能迁移出去,如何办?
var http = require("http");
function start() {
function onRequest(request, response) {
console.log("Request received.");
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;
server.js修改一下,把启动web服务器的代码移到start方法体中,
使用 exports.start = start 向外部暴露web启动方法
习惯上,我们使用index.js来启动app,代码如下:
var server = require("./server"); server.start();
执行 node index.js 效果一样!
3.完善web服务器的路由功能
可以发现,访问8888的任意资源都会返回helloworld,why?
这就说明我们这个服务器其实是缺少路由功能的,也可以说,路由功能使用基本的if else即可实现,我们先获取request请求路径
var http = require("http"); var url = require("url"); function start() { function onRequest(request, response) { var pathname = url.parse(request.url).pathname; console.log("Request for " + pathname + " received."); 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;
使用url即可获取资源请求路径。
习惯上,使用router.js来负责路由分发:
function route(pathname) { console.log("About to route a request for " + pathname); } exports.route = route;
server.js 导入route模块, start方法调用route方法即可,
当然,route其实应该作为服务器的一个组件传入进去,我们可以这样:
var http = require("http"); var url = require("url"); function start(route) { function onRequest(request, response) { var pathname = url.parse(request.url).pathname; console.log("Request for " + pathname + " received."); route(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;
而index.js相应的:
var server = require("./server"); var router = require("./router"); server.start(router.route);
事实上,我们现在的路由器还未有任何处理请求的功能,而根据经验,处理各种请求,应该有对应的程序块,
如果在rout方法体中使用if else,那么代码结构将会极其丑陋
js的函数式编程给了我们一个启示——可以将不同的请求封装在一个requestHandler中,然后暴露出来供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;
index.js入口脚本来载入handler
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);
handler就是一个字典,针对不同请求,执行不同的处理逻辑
相应改变的,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;
至此,一个简单的路由功能就优雅的实现了!
路由功能实现了,但是每个请求响应全都是hello world,这个处理起来也不复杂,只需要让handler返回结果,然后response将结果输出出去即可!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了前端 JavaScript运行原理,JavaScript引擎是一个计算机程序,它的主要作用是JavaScript运行时将源码编译为机器码。每个主流Web浏览器都有自己的JavaScript引擎,它通常由web浏览器供应商开发,接下来一起来看看文章的详细内容吧
这篇文章主要给大家介绍是linux中node卸载与安装步骤,小编认为是比较容易简单上手的,因此分享给大家作参考,有这方面需要的朋友可以看看,下面就跟随小编一起来了解一下吧。
在实际的项目开发中,模糊搜索还是比较常见的需求,因此这篇文章就主要给大家分享用JS怎样实现本地模糊搜索的功能,下文提到了Object.assign()、filter()、indexOf()等方法的使用,对大家学习JavaScript有一定的参考价值,有需要的朋友可以参考。
这篇文章主要介绍了vue3函数setUp和reactive函数的相关知识及setup函数和reactive函数的注意点,通过具体代码给大家介绍的非常详细,需要的朋友可以参考下
这篇文要给大家介绍的是JavaScript原型链,文章主要介绍内容有构造函数、属性Prototype、属性Prototype、属性__proto__、访问原型上的方法等问题,需要的朋友可以参考一下文章的详细内容
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008