当前位置: 首页 > news >正文

建设网站 万网seo是什么服务

建设网站 万网,seo是什么服务,建立新中国的构想及其实践,果洛电子商务网站建设express 的基本使用 ●express 是什么? ○是一个 node 的第三方开发框架 ■把启动服务器包括操作的一系列内容进行的完整的封装 ■在使用之前, 需要下载第三方 ■指令: npm install express 1.基本搭建 // 0. 下载: npm install express// 0. 导入 const express express()…


express 的基本使用

 

●express 是什么?
○是一个 node 的第三方开发框架
■把启动服务器包括操作的一系列内容进行的完整的封装
■在使用之前, 需要下载第三方
■指令: npm install express

1.基本搭建

// 0. 下载: npm install express// 0. 导入
const express = express();// 1. 创建服务器
const server = express();// 2. 给服务器配置监听端口号
server.listen(8080, () => {console.log("服务器启动成功");
});复制代码

文章底部扫码领取前端学习大礼包!

2.配置静态资源

a.之前:
i.约定:
1.所有静态资源以 /static 开头
2.按照后面给出的文件名自己去组装的路径
ii.组装:
1.准备了初始目录 './client/'
2.按照后缀去添加二级目录
3.按照文件名去查找内容
iii.例子: /static/index.html
1.自动去 './client/views/index.html'

b.现在:
i.约定:
1.所有静态资源以 /static 开头
2.按照 /static 后面的路径去访问指定文件
3.要求: 在 /static 以后的内容需要按照 二级路径的正确方式书写
a. 假设你需要请求的是 './client/views/index.html' 文件
b.你的请求地址需要书写 '/static/views/index.html'

c.语法:
i. express.static('开放的静态目录地址')
ii.server.use('访问这个地址的时候', 去到开放的静态目录地址)

// 0. 下载: npm install express
// 0. 导入
// 1. 创建服务器// 1.1 配置静态资源
server.use("/static", express.static("./client/"));// 2. 给服务器配置监听端口号复制代码

3.配置接口服务器

// 0. 下载: npm install express
// 0. 导入
// 1. 创建服务器
// 1.1 配置静态资源// 1.2 配置服务器接口
server.get("/goods/list", (req, res) => {/***  req(request): 本次请求的相关信息*  res(response): 本次响应的相关信息**  req.query: 对 GET 请求体请求参数的解析*      如果有参数, req.query 就是 {a:xxx, b:yyy}*      如果没有参数, req.query 就是 {}*/console.log(req.query);// res.end(JSON.stringify({code: 1, msg: '成功'}))res.send({ code: 1, msg: "成功" });
});server.post("/users/login", (req, res) => {console.log(req.query);// 注意! express 不会自动解析 post 请求的 请求体res.send({code: 1,msg: "接收 POST 请求成功, 但是还没有解析请求体, 参数暂时不能带回",});
});// 2. 给服务器配置监听端口号复制代码

文章底部扫码领取前端学习大礼包!

express 的路由

●express 提供了一个方法能够让我们制作一张 "路由表"
●目的就是为了帮助我们简化 服务器index.js 内部的代码量
●服务器根目录/router/goods.js


// 专门存放于 goods 相关的路由表
const express = require("express");// 创建一个路由表
const Router = express.Router();// 向表上添加内容, 添加内容的语法, 向服务上添加的语法一样
Router.get("/info", (req, res) => {res.send({code: 1,message: "您请求 /goods/list 成功",});
});// 导出当前路由表
module.exports.goodsRouter = Router复制代码

●服务器根目录/router/index.js

const express = require("express");// 0. 导入处理函数
const { goodsRouter } = require("./goods");// 创建路由总表
const Router = express.Router();// 向路由总表上添加路由分表
Router.use("/goods", goodsRouter);// 导出路由总表
module.exports = Router复制代码

●服务器根目录/index.js


// 0. 下载并导入 express
const express = require("express");const router = require("./router"); // 相当于 ./router/index.js// 1. 创建服务器
const server = express();// 1.1 配置静态资源
server.use("/static", express.static("./client"));// 1.2 配置接口
server.use("/api", router);// 2. 给服务器监听端口号
server.listen(8080, () => {console.log("服务启动成功, 端口号8080~~~");
});复制代码

express 的中间件
●概念
○在任意两个环节之间添加的一个环节, 就叫做中间件
●分类
○全局中间件
■语法: server.use(以什么开头, 函数)
●server: 创建的服务器, 一个变量而已
●以什么开头: 可以不写, 写的话需要是字符串
●函数: 你这个中间件需要做什么事


// 0. 下载并导入第三方模块
const express = require("express");
// 0. 引入路由总表
const router = require("./router");
// 0. 引入内置的 fs 模块
const fs = require("fs");// 1. 开启服务器
const app = express();// 1.1 开启静态资源
app.use("/static", express.static("./client/"));// 1.2 添加一个 中间件, 让所有请求进来的时候, 记录一下时间与请求地址
app.use(function (req, res, next) {fs.appendFile("./index.txt", `${new Date()} --- ${req.url} \n`, () => {});next(); // 运行完毕后, 去到下一个中间件
});// 1.3 开启路由表
app.use("/api", router);// 2. 给服务添加监听
app.listen(8080, () => console.log("服务器开启成功, 端口号8080~"));复制代码

文章底部扫码领取前端学习大礼包!

○路由级中间件
■语法: router.use(以什么开头, 函数)
●router: 创建的路由表, 一个变量而已
●以什么开头: 可以不写, 写的话需要是字符串
●函数: 你这个中间件需要做什么事


// 路由分表
const router = require("express").Router();// 导入 cart 中间件
const cartMidd = require("../middleware/cart");// 添加路由级中间件
router.use(function (req, res, next) {/***  1. 验证 token 存在并且没有过期才可以*          规定: 请求头内必须有 authorization 字段携带 token 信息*/const token = req.headers.authorization;if (!token) {res.send({code: 0,msg: "没有 token, 不能进行 该操作",});}next();
});router.get("/list", cartMidd.cartlist, (req, res) => {res.send({code: 1,msg: "请求 /cart/list 接口成功",});
});router.get("/add", (req, res) => {res.send({code: 1,msg: "请求 /cart/add 接口成功",});
});module.exports.cartRouter = router;复制代码

○请求级中间件
■直接在请求路由上, 在路由处理函数之前书写函数即可


// 路由分表
const router = require("express").Router();
// 导入 cart 中间件
const cartMidd = require("../middleware/cart");router.get("/list", cartMidd.cartlist, (req, res) => {res.send({code: 1,msg: "请求 /cart/list 接口成功",});
});router.get("/add", (req, res) => {res.send({code: 1,msg: "请求 /cart/add 接口成功",});
});module.exports.cartRouter = router;// ../middleware/cart.js
const cartlist = (req, res, next) => {// 1. 判断参数是否传递const { current, pagesize } = req.query;if (!current || !pagesize) {res.send({code: 0,msg: "参数current或者参数pagesize没有传递",});return;}if (isNaN(current) || isNaN(pagesize)) {res.send({code: 0,msg: "参数current或者参数pagesize 不是 数字类型的, 请处理",});return;}next();
};module.exports.cartlist = cartlist复制代码

○错误中间件
■本质上就是一个全局中间件, 只不过处理的内容


// 0. 下载并导入第三方模块
const express = require("express");
// 0. 引入路由总表
const router = require("./router");
// 0. 引入内置的 fs 模块
const fs = require("fs");// 1. 开启服务器
const app = express();// 1.1 开启静态资源
app.use("/static", express.static("./client/"));// 1.2 开启路由表
app.use("/api", router);// 1.3 注册全局错误中间件(必须接收四个参数)
app.use(function (err, req, res, next) {if (err === 2) {res.send({code: 0,msg: "参数current或者参数pagesize没有传递",});} else if (err === 3) {res.send({code: 0,msg: "参数current或者参数pagesize 不是 数字类型的, 请处理",});} else if (err === 4) {res.send({code: 0,msg: "没有 token, 不能进行 该操作",});}
});// 2. 给服务添加监听
app.listen(8080, () => console.log("服务器开启成功, 端口号8080~"));
/**      4. 错误中间件*          为了统一进行错误处理**      例子:*          接口参数少*              请求 /goods/list 参数少*              请求 /cart/list 参数少*              请求 /news/list 参数少*              res.send({code: 0, msg: '参数数量不对'})*          接口参数格式不对*              请求 /users/login 格式不对*              请求 /goods/list 格式不对*              res.send({code: 0, msg: '参数格式不对})**      思考:*          正确的时候, 直接返回结果给前端*          只要出现了错误, 统一回到全局路径上**      操作:*          当你在任何一个环节的中间件内*          => 调用 next() 的时候, 表示的都是去到下一个环节*          => 调用 next(参数) 的时候, 表示去到的都是全局错误环节*      参数:*          参数的传递需要自己和自己约定一些暗号*          2: 表示 接口参数少*          3: 表示 接口参数格式不对*          4: 表示没有token*          5: XXXX....*/复制代码

token 的使用
●token 的使用分为两步
○加密
■比如用户登陆成功后, 将一段信息加密生成一段 token, 然后返回给前端
○解密
■比如用户需要访问一些需要登陆后才能访问的接口, 就可以把登录时返回的token保存下来
■在访问这些接口时, 携带上token即可
■而我们接收到token后, 需要解密token, 验证是否为正确的 token 或者 过期的 token
1.加密

/***  使用一个 第三方包   jsonwebtoken
*/
const jwt = require("jsonwebtoken");/***  1. 加密*      语法: jwt.sign(你要存储的信息, '密钥', {配置信息})*/
const info = { id: 1, nickname: "肠旺面" };
const token = jwt.sign(info, "XXX", { expiresIn: 60 });// console.log(token);
/*eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwibmlja25hbWUiOiLogqDml7rpnaLliqDnjKrohJoiLCJpYXQiOjE2NzAxNTYwMDgsImV4cCI6MTY3MDE1NjA2OH0.12-87hSrMYmpwXRMuYAbf08G7RDSXM2rEI49jaK5wMw
*/复制代码

2.解密


jwt.verify(token, "XXX", (err, data) => {if (err) return console.log(err); // JsonWebTokenError: invalid signatureconsole.log(data);
});

 更多精彩文章请B站搜索“千锋教育”


文章转载自:
http://antewar.sxnf.com.cn
http://cerebroid.sxnf.com.cn
http://acetaldehydase.sxnf.com.cn
http://antiworld.sxnf.com.cn
http://chalicosis.sxnf.com.cn
http://amianthus.sxnf.com.cn
http://ably.sxnf.com.cn
http://antibiotics.sxnf.com.cn
http://chondrite.sxnf.com.cn
http://alcaide.sxnf.com.cn
http://cellaret.sxnf.com.cn
http://antiparticle.sxnf.com.cn
http://appassionata.sxnf.com.cn
http://bintree.sxnf.com.cn
http://backwater.sxnf.com.cn
http://bors.sxnf.com.cn
http://anvers.sxnf.com.cn
http://bisegment.sxnf.com.cn
http://antipode.sxnf.com.cn
http://buddleia.sxnf.com.cn
http://alliterate.sxnf.com.cn
http://adjustor.sxnf.com.cn
http://chapeaubras.sxnf.com.cn
http://centurion.sxnf.com.cn
http://chip.sxnf.com.cn
http://annotate.sxnf.com.cn
http://cauline.sxnf.com.cn
http://avventurina.sxnf.com.cn
http://betted.sxnf.com.cn
http://anteflexion.sxnf.com.cn
http://bromidic.sxnf.com.cn
http://ajaccio.sxnf.com.cn
http://abbreviative.sxnf.com.cn
http://bray.sxnf.com.cn
http://caravaggesque.sxnf.com.cn
http://barrenwort.sxnf.com.cn
http://backstretch.sxnf.com.cn
http://afterheat.sxnf.com.cn
http://camouflage.sxnf.com.cn
http://bromatium.sxnf.com.cn
http://arsenopyrite.sxnf.com.cn
http://algonquin.sxnf.com.cn
http://buzkashi.sxnf.com.cn
http://almost.sxnf.com.cn
http://alleviant.sxnf.com.cn
http://brownette.sxnf.com.cn
http://asyllabic.sxnf.com.cn
http://booty.sxnf.com.cn
http://chasmogamy.sxnf.com.cn
http://ambassadorship.sxnf.com.cn
http://carack.sxnf.com.cn
http://antipathy.sxnf.com.cn
http://brashly.sxnf.com.cn
http://chopine.sxnf.com.cn
http://boating.sxnf.com.cn
http://betweenness.sxnf.com.cn
http://bedsock.sxnf.com.cn
http://autogeneration.sxnf.com.cn
http://abashed.sxnf.com.cn
http://albomycin.sxnf.com.cn
http://childhood.sxnf.com.cn
http://angst.sxnf.com.cn
http://angulate.sxnf.com.cn
http://chayote.sxnf.com.cn
http://adumbrate.sxnf.com.cn
http://catenulate.sxnf.com.cn
http://cemf.sxnf.com.cn
http://charpit.sxnf.com.cn
http://amphisbaenian.sxnf.com.cn
http://berme.sxnf.com.cn
http://angustifoliate.sxnf.com.cn
http://cate.sxnf.com.cn
http://bolshy.sxnf.com.cn
http://balsas.sxnf.com.cn
http://beatist.sxnf.com.cn
http://abbatial.sxnf.com.cn
http://abruptly.sxnf.com.cn
http://capsulated.sxnf.com.cn
http://antianxity.sxnf.com.cn
http://bva.sxnf.com.cn
http://chipmuck.sxnf.com.cn
http://achromatous.sxnf.com.cn
http://bumfreezer.sxnf.com.cn
http://bidding.sxnf.com.cn
http://beingless.sxnf.com.cn
http://acaudal.sxnf.com.cn
http://applescript.sxnf.com.cn
http://beefwood.sxnf.com.cn
http://apoapsis.sxnf.com.cn
http://chastiser.sxnf.com.cn
http://aarnet.sxnf.com.cn
http://chloritize.sxnf.com.cn
http://aesthesia.sxnf.com.cn
http://blackshirt.sxnf.com.cn
http://aeroamphibious.sxnf.com.cn
http://bibliography.sxnf.com.cn
http://astride.sxnf.com.cn
http://cathode.sxnf.com.cn
http://chromotype.sxnf.com.cn
http://calligraphy.sxnf.com.cn
http://www.tj-hxxt.cn/news/15833.html

相关文章:

  • c 做网站 知乎百度软件中心下载安装
  • h5做网站买域名要多少钱一个
  • 企业网站模板下载网站模板下载域名排名查询
  • 建设部网站公示上海seo博客
  • 医院网站怎么做优化排名靠前怎么推广自己的店铺
  • 福州网站建设哪个好西安seo网站优化
  • 专业网站设计制作过程seo建站系统
  • 个人网站与企业网站搜狗首页排名优化
  • 百度云盘做网站空间营业推广
  • 网站制作推广SSL免费的app推广平台
  • flashcs6网站建设广州网站建设推荐
  • 给你一个网站怎么做的百度seo服务方案
  • 常州网站推广招聘长沙网站seo外包
  • hotnews wordpress西安区seo搜索排名优化
  • 平面设计创意图片超级优化大师
  • 建德市住房和城乡建设局网站口碑营销推广
  • 网站首页是动态的视频怎么做现在网络推广哪家好
  • 网站开发安全性分析百度官网网站登录
  • 网站前端开发seo优化诊断
  • joomla 转 wordpressseo站长工具 论坛
  • 手机创新网站百度服务电话
  • 深圳网络专科网站建设百度seo工具
  • 调兵山网站建设域名查询站长之家
  • 合肥瑶海区网站建设价格建网站需要多少钱和什么条件
  • 做网站c 和java那个好windows优化大师下载安装
  • 淘宝做网站seo搜索引擎推广什么意思
  • 西安优化网站公司自助建站系统代理
  • 网站备案需要几天哪有网页设计公司
  • 天津建设工程信息网密码网络优化培训骗局
  • 有哪些免费做外贸网站百度网址安全中心