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. linux下文件解压缩中文乱码问题的解决

    将带中文文件名的压缩文件上传到服务器,使用unzip解压后,文件名乱码: 临时解决方法: 通过unzip行命令解压,指定字符集unzip -O CP936 xxx.zip (用GBK, GB18030 ...

  2. python检查是否是闰年

    检查的依据: 闰年可以被4整除不能被100整除,或者可以被400整除. year = int(input("请输入年份:")) if year % 4 == 0 and year ...

  3. 构造分组背包(CF)

    Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of t ...

  4. 关于爬虫的日常复习(17)——scrapy系列1

  5. Qt Installer Framework翻译(5-2)

    创建在线安装程序 联机安装程序获取二进制安装文件中的内容以及存储库描述(Updates.xml).请创建一个存储库,并将其上传到Web服务器.然后在用于创建安装程序的config.xml文件中指定存储 ...

  6. 【python系统学习07】一张图看懂字典并学会操作

    点击跳转 - 原文地址 数据类型 - 字典(dict) 目录: 一张图get字典 字典是什么 js的对象 字典长啥样 语法伪代码 示例demo 语法成像 字典怎么用 字典长度获取--len函数 提取字 ...

  7. 造轮子-toast组件的实现(下)

    1.解决 toast 中传入 html 的问题,通过假的 slot 来实现 // plugins.js toast.$slots.default = [message] // toast.vue &l ...

  8. CSS-04-层叠选择器

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. 动态规划-Dynamic Programming(DP)

    动态规划 动态规划方法心得 ​ 动态规划是一般的面试.笔试中的高频算法题,熟练掌握必要的.动态规划的中心思想是在解决当前问题时,可以由之前已经计算所得的结果并结合现在的限制条件递推出结果.由于此前的计 ...

  10. java web 项目中基础技术

    1. 选择版本控制器(git, svn) 2. 用户登录的时候, 你需要进行认证, 权限受理 可以使用 spring shiro 框架,进行上面的工作 3. 过滤器(filter),监听器(liste ...