Code Monkey home page Code Monkey logo

hehe1111.github.io's People

Contributors

hehe1111 avatar

Watchers

 avatar

hehe1111.github.io's Issues

Express 4.x 笔记

Express 4.x 笔记

API 分类

  • express.xxx 内置中间件
  • app.xxx 应用设置(如:挂载路由处理 HTTP 请求、配置中间件、注册模板引擎、渲染视图等)
  • request.xxx 操作请求
  • response.xxx 操作响应
  • router.xxx 操作路由

API 概要

express()

起一个主应用或者子应用

  • express
    • .json([options]) 请求第四部分 -> body JSON 对象
    • .urlencoded([options]) 请求第四部分 -> body JSON 对象
    • .raw([options]) 请求第四部分 -> body Buffer 对象
    • .text([options]) 请求第四部分 -> body 字符串
    • .Router([options]) 可用于实现嵌套路由
    • .static(root, [options]) 起一个静态资源服务器

Application

  • 重点掌握 app.use([path,] callback [, callback...])
  • 中间件的调用顺序由注册顺序决定。因此中间件的注册顺序很重要
  • 错误处理中间件接受个参数
  • app.get('xxx', (requset, response) => {/* 函数内 */})app 对象在函数内可以通过 request.appresponse.app 来获取
  • app.get()
    • 作用一:读取应用配置
    • 作用二:处理 GET 请求

Request

处理请求

Response

处理响应

Router

可以认为是一个简化版的 Express 应用

Koa 与 Express 的对比

Koa 与 Express 的对比

中间件执行顺序不同

  • Express 线型 开始 -> f1 -> f2 -> f3 -> ... -> f(last) -> 结束
  • Koa U 型 开始 -> f1 -> f2 -> f3 -> ...-> f(last) -> ... -> f3 -> f2 -> f1 -> 结束

语法不同

  • Express 使用回调函数 next()
  • Koa1 使用 generator
  • Koa2 使用 async/await

有无内置中间件

  • Express 有内置中间件
  • Koa 没有内置中间件

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.