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

略。

最新文章

  1. Object-C与Swift混编
  2. 在有EditText控件的AlertDialog对话框中自动弹出输入法
  3. poj2780Linearity(多点共线)
  4. D2 前端技术论坛总结(上)
  5. 基数排序(RadixSort)
  6. java SSH整合配置
  7. Jasper_table_resolve get multiple copies of table in detail band issue
  8. winform —— 对话框和流及打印
  9. UML视图(四)状态图
  10. [网络]_[0基础]_[使用putty备份远程数据]
  11. 一个快速、高效的Levenshtein算法实现——代码实现
  12. java线程池技术(一):ThreadFactory与BlockingQueue
  13. MAC终端神器iterm2——告别黑白
  14. ZJOI2019Day1AFO记
  15. MVC使用记录
  16. Umbraco 资源推荐
  17. 【Spring】26、利用Spring的AbstractRoutingDataSource解决多数据源,读写分离问题
  18. [笔记] imooc《JavaScript深入浅出》对象与函数
  19. XHTML 簡介
  20. 极限编程核心价值:尊重(Respect)

热门文章

  1. qcow2、raw、vmdk等镜像格式的比较和基本转换
  2. Python中的str与bytes之间的转换的三种方法
  3. python偏函数的运用
  4. [分布式系统学习]阅读笔记 Distributed systems for fun and profit 抽象 之二
  5. Nginx 安装 --编译模块参数
  6. codeforces 761D - Dasha and Very Difficult Problem
  7. POJ 1061 - 青蛙的约会 - [exgcd求解一元线性同余方程]
  8. Gym - 101628F Find the Inn dijkstra,读边时计算新权值
  9. CodeForces - 583D Once Again... LIS 循环
  10. 用PHP编写一个APP的API