原生http模块与使用express框架对比
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框架对比的更多相关文章
- Nodejs学习笔记(3) 创建服务器:Web 模块(http)与 express 框架
目录 参考资料 1. 使用 http 模块创建服务器 1.1 实现思路及代码 1.2 HTTP 结构 1.2.1 Request中的重要字段 1.2.2 Response 头信息:文件类型.状态码.连 ...
- http协议、模块、express框架以及路由器、中间件和mysql模块
一.http协议 是浏览器和web服务器之间的通信协议 1.通用头信息 request url:请求的url,向服务器请求的数据 request method:请求的方式 get.post sta ...
- express框架路由配置及congtroller自动加载
express框架在node官方推荐的一个框架,关于如何入门的文章,已经很多了,我就不在累赘了,本文的核心是如何修改文件使得更接近一个MVC的框架 express原生是通过require的方式实现了模 ...
- Express框架学习总结
最近学了Express框架,在学习的过程中,参考了一些资料,感觉Express框架比原生Node.js好用多了.下面我将我学习总结的内容如下: Express中文网 http://www.ex ...
- Android &Swift iOS开发:语言与框架对比
转载自:http://www.infoq.com/cn/articles/from-android-to-swift-ios?utm_campaign=rightbar_v2&utm_sour ...
- node.js之express框架入门篇
一.express框架简介 express框架是后台的Node框架,在后台的受欢迎的程度,和jQuery一样 英语官网:http://expressjs.com/ 中文官网:http://www.ex ...
- 22.Express框架——2019年12月19日
2019年12月19日14:16:36 1. express简介 1.1 介绍 Express框架是后台的Node框架,所以和jQuery.zepto.yui.bootstrap都不一个东西. Exp ...
- Node.js Express 框架学习
转载:http://JavaScript.ruanyifeng.com/nodejs/express.html#toc0 感觉很牛的样子,不过觉得对初学者没太大用,里面很多例子用的api都没有详细的说 ...
- Node.js Express 框架
Node.js Express 框架 Express 简介 Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用,和丰富的 HTTP ...
随机推荐
- GitStack系统RCE漏洞学习
漏洞简介 漏洞简情 漏洞程序 GitStack 影响版本 <=2.3.10 漏洞类型 RCE 漏洞评价 高危 漏洞编号 CVE-2018-5955 漏洞程序介绍 GitStack是一款基于Pyt ...
- Ubuntu下Chrome运行Silverlight程序
Ubuntu 14.04.1下运行Terminal,安装Pipelight输入以下命令: sudo add-apt-repository ppa:pipelight/stable sudo apt-g ...
- 使用jetty的continuations实现"服务器推"
在实际的开发中,我们可能会有这样的场景:许多客户端都连接到服务器端,当有某个客户端的消息的时候,服务器端会主动"推"消息给客户端,手机app的推送是一个典型的场景(IOS的推送都是 ...
- 7.18 python进程间数据共享
# 管道# 数据共享 Manager# 进程池和回调函数 ! # !/usr/bin/env python # !--*--coding:utf-8 --*-- # !@Time :2018/7/18 ...
- 服务端渲染(ssr)初了解
之前接触的比较多的是SPA单页面应用,前端路由渲染,对于node服务端渲染刚开始了解到,服务端渲染的话相对于SPA来说有助于SEO优化,首屏加载更快. 和之前的SPA项目不同,之前公司spa的发布部署 ...
- myeclipse乱码/GBK只支持中文
Windows>>Pereferences>>General>Editors>>Spelling>>Encoding选项下选择other,然后输入 ...
- 计蒜客 31452 - Supreme Number - [简单数学][2018ICPC沈阳网络预赛K题]
题目链接:https://nanti.jisuanke.com/t/31452 A prime number (or a prime) is a natural number greater than ...
- MySQL升级方法一
1.在目标服务器上安装新版本的mysql数据库. 1)解压缩新版mysql数据库压缩包并cp到指定目录: [root@faspdev mnt]# tar -zxvf mysql-5.6.24-linu ...
- 简单利用gulp-babel搭建es6转es5环境
es6是什么?借着这个话题,我想说:身为web前端的工作者连es6没听说,转行吧. demo的代码如下: 源码下载 或者 gitclone地址: git@git.oschina.net:sisheb/ ...
- 【Python】【Web.py】python web py入门-5-请求处理(下)
前面一篇,我们演示了如何获取GET和POST请求的参数信息,这篇我们介绍如何获取请求的头部信息,这个方法我们在前面一篇文章已经给出了.直接来看一个例子,首先,我们在hello.py文件新增一个方法,用 ...