node的http创建服务与利用Express框架有何不同

原生http模块与使用express框架对比:
const http = require("http"); let server = http.createServer(function (req, res) { // 服务器收到浏览器web请求后,打印一句话 console.log("recv req from browser"); // 服务器给浏览器回应消息 res.end("hello browser"); }); server.listen();
服务器执行:
$ node app.js
recv req from browser 使用express框架后:
const http = require("http"); const express = require("express"); // app是一个函数 let app = express(); http.createServer(app); // 路由处理,默认是get请求 app.get("/demo", function(req, res) { console.log("rcv msg from browser"); res.send("hello browser"); res.end(); }); app.listen();
服务器执行:
$ node app.js
rcv msg from browser express到底是什么?
function createApplication() { var app = function (req, res, next) { app.handle(req, res, next); }; mixin(app, EventEmitter.prototype, false); mixin(app, proto, false); // expose the prototype that will get set on requests app.request = Object.create(req, { app: { configurable: true, enumerable: true, writable: true, value: app } }) // expose the prototype that will get set on responses app.response = Object.create(res, { app: { configurable: true, enumerable: true, writable: true, value: app } }) app.init(); return app; } 总结:
.express框架简单封装了node的http模块,因此,express支持node原生的写法。express的重要意义在于:支持使用中间件 + 路由 来开发web服务器逻辑。
.express()就是createApplication()

略。

原生http模块与使用express框架对比的更多相关文章

  1. Nodejs学习笔记(3) 创建服务器:Web 模块(http)与 express 框架

    目录 参考资料 1. 使用 http 模块创建服务器 1.1 实现思路及代码 1.2 HTTP 结构 1.2.1 Request中的重要字段 1.2.2 Response 头信息:文件类型.状态码.连 ...

  2. http协议、模块、express框架以及路由器、中间件和mysql模块

    一.http协议 是浏览器和web服务器之间的通信协议 1.通用头信息 request url:请求的url,向服务器请求的数据 request method:请求的方式   get.post sta ...

  3. express框架路由配置及congtroller自动加载

    express框架在node官方推荐的一个框架,关于如何入门的文章,已经很多了,我就不在累赘了,本文的核心是如何修改文件使得更接近一个MVC的框架 express原生是通过require的方式实现了模 ...

  4. Express框架学习总结

    最近学了Express框架,在学习的过程中,参考了一些资料,感觉Express框架比原生Node.js好用多了.下面我将我学习总结的内容如下: Express中文网     http://www.ex ...

  5. Android &Swift iOS开发:语言与框架对比

    转载自:http://www.infoq.com/cn/articles/from-android-to-swift-ios?utm_campaign=rightbar_v2&utm_sour ...

  6. node.js之express框架入门篇

    一.express框架简介 express框架是后台的Node框架,在后台的受欢迎的程度,和jQuery一样 英语官网:http://expressjs.com/ 中文官网:http://www.ex ...

  7. 22.Express框架——2019年12月19日

    2019年12月19日14:16:36 1. express简介 1.1 介绍 Express框架是后台的Node框架,所以和jQuery.zepto.yui.bootstrap都不一个东西. Exp ...

  8. Node.js Express 框架学习

    转载:http://JavaScript.ruanyifeng.com/nodejs/express.html#toc0 感觉很牛的样子,不过觉得对初学者没太大用,里面很多例子用的api都没有详细的说 ...

  9. Node.js Express 框架

    Node.js Express 框架 Express 简介 Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用,和丰富的 HTTP ...

随机推荐

  1. Ubuntu 12.04 Openstack Essex 安装(单节点)

    这是陈沙克一篇非常好的博文,当时在进行openstack排错的时候,多亏了这篇文章里面有些内容 帮我找到了问题的所在: 原文:http://www.chenshake.com/ubuntu-12-04 ...

  2. psr-4

    自动加载: <?php function autoload($className) { $className = ltrim($className, '\\'); $fileName = ''; ...

  3. [SCOI2011]飞镖[数学模拟]

    2335: [SCOI2011]飞镖 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 482  Solved: 152[Submit][Status][ ...

  4. gradle项目,连同依赖一起打jar包

    在build里加入以下配置(如果不是一个可执行的jar包的话就不用配置Main-Class属性): def mainClassName = "你需要执行的main方法所在的的包名+类名&qu ...

  5. 近期在看的一段JS(谁能看出我想实现什么功能)

    示例代码: <script type="text/javascript"> !function(){ var e=/([http|https]:\/\/[a-zA-Z0 ...

  6. Nginx配置认证登录

    本配置实现Nginx认证登录以免重要信息暴露在公网 日志收集ELK展示工具kibana免费版不支持密码验证,需要设置Nginx反向代理然后关闭kibana默认端口5601使用Nginx端口登录进行用户 ...

  7. 南阳理工大学oj 题目15 括号匹配(二)

    括号匹配(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:6   描述 给你一个字符串,里面只包含"(",")","[&qu ...

  8. POJ 1700 - Crossing River

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13982   Accepted: 5349 Description A gr ...

  9. (转载)准确率(accuracy),精确率(Precision),召回率(Recall)和综合评价指标(F1-Measure )-绝对让你完全搞懂这些概念

    自然语言处理(ML),机器学习(NLP),信息检索(IR)等领域,评估(evaluation)是一个必要的工作,而其评价指标往往有如下几点:准确率(accuracy),精确率(Precision),召 ...

  10. This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in

    地址:http://stackoverflow.com/questions/18852983/eclipse-reports-rendering-library-more-recent-than-ad ...