express(2)
来源:互联网 发布:淘宝产品营销方案 编辑:程序博客网 时间:2024/06/04 00:35
var http = require("http");var Application = function(){//js当中没有明确的构造器概念,都是以首字母大写来区分 //保存路由 this.router = [{ path:"*", fn:function(req,res){ res.writeHead(404,{"Content-type":"text/plain"}); //req.method:请求的方法(get/post) res.end("Cannot " + req.method + ' ' + req.url); } }];}Application.prototype = { use:function(path,fn){//path:传入的路由名字,fn:传入的路由回调函数 this.router.push({ path:path, fn:fn }); }, listen:function(port){ var self = this;//区分作用域,保存this(application)对象 http.createServer(function(req,res){ //我需要指向谁,而这里的this又指向谁呢? //执行到这里的时候就有必要判断路由了 for(var i = 1;i < self.router.length;i++){ if(req.url == self.router[i].path){ return self.router[i].fn(req,res); } } return self.router[0].fn(req,res); }).listen(Number(port));//判断类型 }}exports = module.exports = Application;
阅读全文
0 0
- express(2)
- express入门(2)-路由
- nodejs--express开发个人博客(2)
- express + node.js 入门(2)
- vue+express:搭建个人博客(2)
- express-2-中间件
- express-10-登录2
- sqlserver2005(Express版),
- express入门(1)
- express源码(1)
- express源码(3)
- express
- express
- Express
- express
- Express
- express
- Express
- opencv was not found in the pkg-config search path
- Java序列化与反序列化
- PAT Advanced 1044. Shopping in Mars (25)
- 分析器错误消息: 无法识别的属性“targetFramework”。请注意属性名称区分大小写。
- C和指针之字符串编程练习1
- express(2)
- 广搜模板
- 微信小程序循环:Wx:for
- 321. Create Maximum Number
- HTTP 错误 500.21
- 1047. 编程团体赛(20)
- Vue.js初体验
- 第一章 习题
- vba基础打卡5