zoukankan      html  css  js  c++  java
  • node -- express框架

    express

    node的一个框架

    安装express
    cnpm  install express -S
    
    引入
    const express = require("express");
    
    应用
    const express = require("express");
    const app = express();
    
    app.get("/",function(req,res){
        res.end("hello")
    });                             //地址栏127.0.0.1 返回hello
    app.get("/app",function(req,res){   //""里面不区分大小写,程序自上向下执行,当找到符合条件的路由时,不会向下继续执行
        res.end("app.content");
    })								//地址栏127.0.0.1/app 返回app.content
    app.listen(80,()=>{
        console.log("success");
    })
    
    
    程序自上向下执行,当找到符合条件的路由时,不会向下继续执行
    
    const express = require("express");
    const app = express();
                            
    app.get("/app",function(req,res){   //""里面不区分大小写,
        res.end("app1");
    })								//地址栏127.0.0.1/app 返回app1 不会再向下执行
    app.get("/app",function(req,res){   //""里面不区分大小写,
        res.end("app2");
    })
    app.listen(80,()=>{
        console.log("success");
    })
    
    //可以通过next继续向下查找
    const express = require("express");
    const app = express();
    
    app.get("/app",function(req,res,next){
        console.log(111);
        // res.end("hello")
        next();          //调用next();
    });
    app.get("/app",function(req,res){
        console.log(222);
        res.end("app.content");
    })
    app.listen(80,()=>{
        console.log("success");
    })
    
    
    get访问所有地址
    const express = require("express");
    const app = express();
    app.get("*",function(req,res){
        res.end("app.content");
    })                       // 所有地址返回的都是app.content , 
    app.get("/app",function(req,res){
        res.end("goodbye");
    })						// 不生效==
    app.listen(80,()=>{
        console.log("success");
    })
    
    
    
    const express = require("express");
    const app = express();
    app.get("/app",function(req,res){
        res.end("app.content");
    })                       // /app 返回的是 app.content
    app.get("*",function(req,res){
        res.end("goodbye");
    })						// 其他地址 返回的是 goodbye
    app.listen(80,()=>{
        console.log("success");
    })
    
    
    post形式访问
    const express = require("express");
    const app = express();
    
    app.post("/app",function(req,res){
        res.end("post?");
    })
    
    app.listen(80,()=>{
        console.log("success");
    })
    
    
    all 不限制访问形式,不限制访问地址
    const express = require("express");
    const app = express();
    
    app.all("*",function (req,res) {
        res.end("all")
    })
    
    app.listen(80,()=>{
        console.log("success");
    })
    
    
    get 接收值
    const express = require("express");
    const app = express();
    // http://127.0.0.1/sum?a=1&b=2
    app.get("/sum",function(req,res){
        console.log(req.query);   // { a: '1', b: '2' }
        res.end();
    })                           
    
    // http://127.0.0.1/sum/1/2
    app.get("/sum/:id/:type",function (req,res) {
        console.log(req.params);  // { id: '1', type: '2' }
        res.end();
    })
    
    
    app.listen(80,()=>{
        console.log("success");
    })
    
    
    post接收值
    **json**
         1、传:
            const xhr = new XMLHttpRequest();
            xhr.open("post","http://127.0.0.1/sum?a=1&b=200");
            xhr.setRequestHeader("content-type","application/json")
            // xhr.send(JSON.stringify({e:12}));
            xhr.send("{"e":12}");
            xhr.onload = function () {
                console.log(xhr.responseText);
            }
        2、收:
            app.use(bodyParser.json());
            req.body;
    
    **urlencoded**
         1、传:
                const xhr = new XMLHttpRequest();
                xhr.open("post","http://127.0.0.1/sum?a=1&b=200");
                xhr.setRequestHeader("content-type","application/x-www-form-urlencoded")
                xhr.send("c=3&d=4");
                xhr.onload = function () {
                    console.log(xhr.responseText);
                }
        2、收:
            1、安装body-parser
                cnpm install body-parser -S
            2、引入
                const bodyParser = require("body-parser");
            3、设置
                app.use(bodyParser.urlencoded());
            4、req.body来接收
    
    

    Express 中间件----body-parser

    body-parser是一个HTTP请求体解析中间件,使用这个模块可以解析JSON、Raw、文本、URL-encoded格式的请求体,Express框架中就是使用这个模块做为请求体解析中间件。

    安装
    cnpm install body-parser
    
    引入
    const express = require('express')
    //获取模块
    const bodyParser = require('body-parser')
    
    const app = express()
    
    应用
    1 bodyParser.json(options): 解析json数据
    2 bodyParser.raw(options): 解析二进制格式(Buffer流数据)
    3 bodyParser.text(options): 解析文本数据
    4 bodyParser.urlencoded(options): 解析UTF-8的编码的数据。
    
    app.use(express.static('public'));
    为了提供对静态资源文件(图片,css,js文件)的服务,请使用Express内置的中间函数express.static.
    
    传递一个包含静态资源的目录给express.static中间件用于立即开始提供文件。 比如用以下代码来提供public目录下的图片、css文件和js文件:
    app.use(express.static('public'));
    
    
    
    路由
    const router = express.Router() //创建一个路由对象
    
    router.get('/login',(req,res)=>{
      res.send('login ok ')
    })
    
    router.post('/reg',(req,res)=>{
      res.send('reg ok ')
    })
    module.exports = router   
    
        //server.js
        const express = require('express')
        const app  = express()  //实例化express 
        const userRouter= require('./userRouter')
        app.use('/user',userRouter)
        app.listen(8888,()=>{
          console.log('服务器 启动')
        })
    

    代理跨域

    //前端页面jq方法调用
    let url='http://localhost:8888/test'
    $.get(url,(data)=>{
      console.log(data)
    })
    
    const express = require('express')
    const app  = express()  //实例化express 
    const cors = require('cors')
    const  request = require('request') 
    app.use(cors())
    // 通过cors 来解决express跨域
    app.get('/test',(req,res)=>{
    
      // 发起服务器端请求
      let url='http://ustbhuangyi.com/music/api/getDiscList?g_tk=1928093487&inCharset=utf-8&outCharset=utf-8&notice=0&format=json&platform=yqq&hostUin=0&sin=0&ein=29&sortId=5&needNewCode=0&categoryId=10000000&rnd=0.03860120327310179'
      request(url,(err,response,body)=>{
        console.log(body)
        res.send(body)
      })
      
    })
    
    app.listen(8888,()=>{
      console.log('服务器 启动')
    })
    
  • 相关阅读:
    windwos8.1英文版安装SQL2008 R2中断停止的解决方案
    indwows8.1 英文版64位安装数据库时出现The ENU localization is not supported by this SQL Server media
    Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds
    SQL数据附加问题
    eclipse,myeclipse中集合svn的方法
    JAVA SSH 框架介绍
    SSH框架-相关知识点
    SuperMapRealSpace Heading Tilt Roll的理解
    SuperMap iserver manage不能访问本地目的(IE9)
    Myeclipse中js文件中的乱码处理
  • 原文地址:https://www.cnblogs.com/zhaoxinran997/p/12180853.html
Copyright © 2011-2022 走看看