7.Express

Express简介

是什么:基于nodeJs开发的一个框架
好 处:加快项目开发,便于团队协作

使用

创建项目文件夹

安装express框架

  • 安装命令
npm install express

创建server服务

创建app.js入口文件

  • 1.引入模块
  • 2.创建web服务器
  • 3.创建路由(http创建时:是创建监听并请求事件,这里框架引出路由概念)
  • 4.启动服务
// 1.引入模块
const express=require('express')
// 2.创建web服务器
const app=express()
//3.创建路由(http创建时:创建监听并请求事件)
app.get('/',function(req,res){
    //响应字符串
    res.send("响应成功"); //自动识别 直接响应,意:响应并关闭
})
// 4.启动服务
app.listen('2002',function(){
    console.log("ok: http://localhost:2002 ");
})

Express路由

路由用于确定应用程序如何响应对特定端点的客户机请求,包含一个 URI(或路径)和一个特定的 HTTP 请求方法(GET、POST 等)

基本语法

  • 发送get请求app.get()
  • 发送post请求app.post()
  • 发送任意请求app.all()
  • 非完全匹配url路径发送任意请求app.use()
//app.HTTP请求类型(请求路径,回调函数)

//1.发送get请求
app.get('/',function(req,res){})
//2.发送post请求
app.post('/',function(req,res){})
//3.发送任意请求
app.all('/',function(req,res){})
//4.非完全匹配url路径发送任意请求
app.use('/',function(req,res){})
  • Express 定义了如下HTTP 请求类型方法: get, post, put, head, delete, options, trace, copy, lock, mkcol, move, purge, propfind, proppatch, unlock, report, mkactivity, checkout, merge, m-search, notify, subscribe, unsubscribe, patch, search, 和 connect。
posted @ 2021-11-29 21:36  禾耳  阅读(54)  评论(0)    收藏  举报