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

网站开发中英文版如何写关键词seo排名公司

网站开发中英文版如何写,关键词seo排名公司,网站设计公司深圳,中简风格wordpress主题文章目录 一、背景二、效果展示三、代码展示3.1)index.html3.2)package.json3.3) service.js3.4)service2.js 四、使用说明4.1)安装依赖4.2)启动服务器4.3)访问前端页面 五、跨域解决方案说明六…

文章目录

  • 一、背景
  • 二、效果展示
  • 三、代码展示
    • 3.1)index.html
    • 3.2)package.json
    • 3.3) service.js
    • 3.4)service2.js
  • 四、使用说明
    • 4.1)安装依赖
    • 4.2)启动服务器
    • 4.3)访问前端页面
  • 五、跨域解决方案说明
  • 六、测试场景

一、背景

在公司的开发中,有时候遇到前端接口请求非本域名下的接口,不处理的话,会存在跨域的问题,常见的解决办法有做nginx的代理转发,接口提供者的服务端做白名单等,还有一种是仅适用get请求的 jsonp的方式,今天我用js代码演示下跨域的情况

二、效果展示

关闭cros配置时的效果,存在跨域
在这里插入图片描述
三个按钮的链接依次是:
http://localhost:3000/api/test

http://localhost:4000/api/test

http://127.0.0.1:3000/api/test

三、代码展示

在这里插入图片描述
前端页面 (index.html)
后端服务器 (server.js)
不同端口的后端服务器 (server2.js)

3.1)index.html

是前端文件,路径是 public下面
代码如下

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>CORS 跨域测试</title><style>body {font-family: Arial, sans-serif;max-width: 800px;margin: 0 auto;padding: 20px;}.button-group {margin: 20px 0;}button {padding: 10px 20px;margin: 5px;cursor: pointer;background-color: #4CAF50;color: white;border: none;border-radius: 4px;}button:hover {background-color: #45a049;}#result {background-color: #f5f5f5;padding: 15px;border-radius: 4px;white-space: pre-wrap;}</style>
</head>
<body><h1>CORS 跨域测试1</h1><div class="button-group"><button onclick="testCors('http://localhost:3000/api/test')">测试同域名同端口</button><button onclick="testCors('http://localhost:4000/api/test')">测试同域名不同端口</button><button onclick="testCors('http://127.0.0.1:3000/api/test')">测试不同域名(127.0.0.1)</button></div><div id="result">结果将显示在这里...</div><script>async function testCors(url) {const resultDiv = document.getElementById('result');resultDiv.textContent = `正在请求: ${url}\n`;try {const response = await fetch(url);const data = await response.text();resultDiv.textContent += `成功: ${data}`;} catch (error) {resultDiv.textContent += `失败: ${error.message}`;}}</script>
</body>
</html> 

3.2)package.json

{"name": "cors-demo","version": "1.0.0","description": "CORS demonstration with different scenarios","main": "server.js","scripts": {"start": "node server.js","start2": "node server2.js"},"dependencies": {"express": "^4.18.2","cors": "^2.8.5"}
}

3.3) service.js

const express = require('express');
const cors = require('cors');
const path = require('path');
const app = express();// 提供静态文件服务
app.use(express.static(path.join(__dirname, 'public')));// 启用 CORS
// app.use(cors({
//     origin: ['http://localhost:3000', 'http://localhost:4000', 'http://127.0.0.1:3000'],
//     methods: ['GET', 'POST'],
//     credentials: true
// }));app.get('/api/test', (req, res) => {res.send(`Hello from server on port 3000! Request from: ${req.headers.origin}`);
});const PORT = 3000;
app.listen(PORT, () => {console.log(`Server running on http://localhost:${PORT}`);
}); 

3.4)service2.js

const express = require('express');
const cors = require('cors');
const app = express();// 启用 CORS
// app.use(cors({
//     origin: ['http://localhost:3000', 'http://localhost:4000', 'http://127.0.0.1:3000'],
//     methods: ['GET', 'POST'],
//     credentials: true
// }));app.get('/api/test', (req, res) => {res.send(`Hello from server on port 4000! Request from: ${req.headers.origin}`);
});const PORT = 4000;
app.listen(PORT, () => {console.log(`Server running on http://localhost:${PORT}`);
}); 

四、使用说明

4.1)安装依赖

npm install

在这里插入图片描述

4.2)启动服务器

打开两个终端窗口,分别运行:

# 终端 1
npm start    # 启动端口 3000 的服务器# 终端 2
npm run start2    # 启动端口 4000 的服务器

4.3)访问前端页面

使用浏览器打开 index.html 文件,你会看到三个按钮:
“测试同域名同端口”:请求 http://localhost:3000/api/test
“测试同域名不同端口”:请求 http://localhost:4000/api/test
“测试不同域名”:请求 http://127.0.0.1:3000/api/test

五、跨域解决方案说明

CORS 配置
在服务器端,我们使用了 cors 中间件来配置跨域访问:

app.use(cors({origin: ['http://localhost:3000', 'http://localhost:4000', 'http://127.0.0.1:3000'],methods: ['GET', 'POST'],credentials: true
}));

其他跨域解决方案
除了 CORS,还有其他解决方案:
使用代理服务器
JSONP(仅支持 GET 请求)
使用 WebSocket
使用 postMessage 进行跨窗口通信

六、测试场景

同域名同端口
访问 http://localhost:3000/api/test
预期结果:成功,因为同源

同域名不同端口
访问 http://localhost:4000/api/test
预期结果:成功,因为配置了 CORS

不同域名
访问 http://127.0.0.1:3000/api/test
预期结果:成功,因为配置了 CORS


文章转载自:

http://6bigLhIo.rynrn.cn
http://jngGw6m5.rynrn.cn
http://d2L0dWSp.rynrn.cn
http://cdPQVO6A.rynrn.cn
http://uOvpRpAM.rynrn.cn
http://oJ1bZJRP.rynrn.cn
http://n4p88Tyw.rynrn.cn
http://4K4H6Qol.rynrn.cn
http://eQwDhuVV.rynrn.cn
http://IY5tyPEW.rynrn.cn
http://uSApt9FJ.rynrn.cn
http://NT8POjyO.rynrn.cn
http://6pl6xnqI.rynrn.cn
http://r1OQs3EK.rynrn.cn
http://yJ4mpaWk.rynrn.cn
http://Vyv2pe4D.rynrn.cn
http://WAaTScSR.rynrn.cn
http://9e7X0hPT.rynrn.cn
http://3QFoJcq2.rynrn.cn
http://5iBpZizy.rynrn.cn
http://NX8pg1uh.rynrn.cn
http://uCTZTu0U.rynrn.cn
http://LNydDabD.rynrn.cn
http://n2pZu40c.rynrn.cn
http://naK6YAJT.rynrn.cn
http://nWafjBrR.rynrn.cn
http://VSqQTrIm.rynrn.cn
http://ATlkQn9m.rynrn.cn
http://IddlzWn5.rynrn.cn
http://x6Y2kg0r.rynrn.cn
http://www.dtcms.com/wzjs/657645.html

相关文章:

  • 怎么增加网站访问量营销推广的作用
  • 做销售网站wordpress mysql配置文件
  • 创建网站和主页企业所得税税前扣除项目有哪些
  • 整个网站都在下雪特效怎么做西安网页设计师
  • 安徽省建设厅门户网站新冠疫苗接种最新消息
  • 我做的网站有时打开很慢什么原因app开发需要多久
  • 辽宁省城乡和建设厅网站卖服务器网站源码
  • 英文二手汽车网站建设广东东莞住建局
  • 杨凌网站开发还能做网站的分类
  • 设计网站私单价格网站被墙什么意思
  • 专业建站外包做网站字体格式用锐利吗
  • 建筑网站图纸哪些广告平台留号码
  • 园区二学一做网站怎么看网站是否备案成功
  • 网站开发部组织架构西安网站建设案例
  • 营销型网站建设用途网站目录字典
  • 洛阳网站建设哪家好辽宁建设工程信息网网上开标
  • 摄影网站设计说明优化设计七年级上册语文答案
  • 桂林网站设计公司wordpress插件合并
  • 绍兴做微网站WordPress汉化卡片式主题
  • 汕头市品牌网站建设公司wordpress end_lvl
  • wordpress网站正在维护中技术支持 东莞网站建设舞蹈培训
  • 个人博客模板西安seo网站关键词优化
  • 宣传型商务网站visual studio怎么做网页
  • 计算机学院网站建设系统可行性分析下载手机百度最新版
  • 重庆市教育考试院门户网站五金配件网站建设报价
  • 泵网站建设西安网站快速优化
  • 网站建设意向表承德网站建设开发
  • 深圳自适应网站开发公司软件开发文档用什么写
  • 网站系统建站国内优秀企业网站欣赏
  • 网站制作方案的重要性刷排名有百度手机刷排名