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('服务器 启动')
})

node -- express框架的更多相关文章

  1. node express框架基本配置

    node express框架基本配置 初始化项目 express -e 安装依赖包 npm install 安装第三方包 npm install xxx --save-dev dos 运行node a ...

  2. node+express框架中连接使用mysql经验总结

    最近在学习node.js,做了一个练手项目,使用node.js+express框架,配合mysql数据库和前端vue框架开发一个多人文档编辑系统. koa,express,node 通用方法连接MyS ...

  3. node——express框架

    express基于Node.js是一个web开发框架,web框架是为了我们开发更方便,更简洁,更高效. 英文网址 中文网址 安装: npm install express --save express ...

  4. Node Express 4.0 安装

    前言 今天想要用nodejs 写个后台服务,很久之前看过node express 框架,可真当向下手安装的时候,发现好多命令都不记得了.写完后台服务,没事了,总结了下安装过程,记录一下,以便以后查阅方 ...

  5. Node.js、Express框架获取客户端IP地址

    Node.js //传入请求HttpRequest function getClientIp(req) { return req.headers['x-forwarded-for'] || req.c ...

  6. Node.js Express 框架学习

    转载:http://JavaScript.ruanyifeng.com/nodejs/express.html#toc0 感觉很牛的样子,不过觉得对初学者没太大用,里面很多例子用的api都没有详细的说 ...

  7. Node.js Express 框架

    Node.js Express 框架 Express 简介 Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用,和丰富的 HTTP ...

  8. Node.js基于Express框架搭建一个简单的注册登录Web功能

    这个小应用使用到了node.js  bootstrap  express  以及数据库的操作 :使用mongoose对象模型来操作 mongodb 如果没了解过的可以先去基本了解一下相关概念~ 首先注 ...

  9. Node.js Express框架

    Express 介绍 Express是一个最小的,灵活的Node.js Web应用程序框架,它提供了一套强大的功能来开发Web和移动应用程序. 它有助于基于Node Web应用程序的快速开发.下面是一 ...

随机推荐

  1. nginx负载均衡动态自动更新(微博开源模块nginx-upsync-module使用)

    这几天项目有个需求:负载要求能根据节点健康状态动态的增减.nginx自带的upstram已经很强大,而且基于Nginx Upstream配置动态更新已经有很多开源方案,大多数都是基于生成配置文件后进行 ...

  2. 内置3D对象-Unity3D游戏开发培训

    内置3D对象-Unity3D游戏开发培训 作者:Jesai 2018-02-12 19:21:58 五大面板: -Hierachy:当前场景中的物体 图 1-1 -Project:项目中的所有资源 图 ...

  3. Activiti接受任务(receiveTask)

    Activiti接受任务(receiveTask) 作者:Jesai 前言: Activiti接受任务(receiveTask)其实和Activiti的手动任务是差不多的,不过手动任务是直接通过,而A ...

  4. [bzoj2004] [洛谷P3204] [Hnoi2010] Bus 公交线路

    Description 小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依次编号为1到N,相邻公交车站间的距 离均为1km. 作为公交车线路的规划者,小Z调查了市民的需求,决 ...

  5. C语言进阶——全局变量

    全局变量 ·定义在函数外面的变量是全局变量 ·全局变量具有全局的生存期和作用域 ·它们与任何函数都无关 ·在任何函数内部都可以使用它们 全局变量初始化 ·没有做初始化的全局变量会得到0值 ·指针会得到 ...

  6. thinkphp快速入门(学习php框架及代码审计)

    之前想学习php代码审计,但是没有坚持下去,记得当时看到了很多CMS框架采用MVC架构,就嘎然而止了. 为了深入学习下框架,一边看着thinkphp官方文档,一边写个简单的登陆注册页面以加深理解. 官 ...

  7. (转)宽字节编码类型的XSS

    今晚又看了一遍PKAV-心上的瘦子写的xss腾讯系列的例子,收获挺大的,其中对宽字节注入有了更深的了解,又查找了一些相关的资料,整理一下,以备以后查阅 参考文档: http://book.2cto.c ...

  8. Mayor's posters (线段树+离散化)

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

  9. Java程序员学习Go指南(二)

    摘抄:https://www.luozhiyun.com/archives/211 Go中的结构体 构建结构体 如下: type AnimalCategory struct { kingdom str ...

  10. python,读取txt的方法和应用

    1.读取txt内的百度盘地址,循环保存到百度云中(直接访问下方地址) https://www.cnblogs.com/becks/p/11409467.html 2.读取txt内参数,循环执行查询,读 ...