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

官方网站制作搜狗排名优化工具

官方网站制作,搜狗排名优化工具,泰安微信网站建设,学seo如何入门目录 1 修改后端服务地址2 解决跨域问题3 动态获取菜单4 测试后端接口5 前后端联调总结 目前我们已经搭建了TDesign的前端和express的后端,目前是两个独立的应用。通常我们需要把前后端集成在一起,TDesign已经配置了相关的信息,只需要修改后端…

目录

  • 1 修改后端服务地址
  • 2 解决跨域问题
  • 3 动态获取菜单
  • 4 测试后端接口
  • 5 前后端联调
  • 总结

目前我们已经搭建了TDesign的前端和express的后端,目前是两个独立的应用。通常我们需要把前后端集成在一起,TDesign已经配置了相关的信息,只需要修改后端服务地址及端口就能实现先后端互通。

1 修改后端服务地址

vs code打开我们创建的前端工程,找到.env.development文件
在这里插入图片描述
将配置文件的URL修改为我们的后端地址

VITE_BASE_URL = /
VITE_IS_REQUEST_PROXY = true
VITE_API_URL = http://localhost:3000
VITE_API_URL_PREFIX = /api

2 解决跨域问题

如果是使用默认的express启动代码搭建服务,会遇到跨域问题,我们需要引入cors中间件来解决跨域的问题

打开后端工程,安装cors包

npm install cors --save

在这里插入图片描述
修改index.js文件,增加跨域支持的代码

const express = require('express');
const app = express();
const cors = require('cors');
// 定义路由
app.get('/', (req, res) => {res.send('Hello, World!');
});
app.use(cors({origin: 'http://localhost:3002',credentials: true
}));
// 启动服务器
app.listen(3000, () => {console.log('Server is running on port 3000');
});

这里的origin是你的前端的工程的地址

3 动态获取菜单

我们目前的菜单是需要从服务接口获取数据的,要想开发后端接口,需要先创建数据库表,数据库我们选择mysql,以下是建表语句

CREATE TABLE `menu`  (`id` int(0) NOT NULL AUTO_INCREMENT,`path` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,`component` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,`redirect` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,`title` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,`icon` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,`parent_id` int(0) NULL DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE,INDEX `parent_id`(`parent_id`) USING BTREE,CONSTRAINT `menu_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `menu` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

表建好之后,需要先创建一个菜单的路由,在工程的根目录创建一个menu.js文件
在这里插入图片描述
我们后端服务需要访问数据库,需要将数据库的连接拆分到一个文件中,再创建一个database.js文件
在这里插入图片描述
访问数据库需要用到mysql库,安装一下

npm install mysql --save

在这里插入图片描述
在database.js里输入数据库连接代码

const mysql = require('mysql');// 创建数据库连接池
const pool = mysql.createPool({host: 'localhost',user: 'root',password: '******',database: 'diancan',
});module.exports = pool;

在配置文件中输入数据库的名称、用户名、密码和访问地址

然后在menu.js中输入获取菜单的方法

const express = require('express');
const router = express.Router();
const pool = require('./database');function fetchMenuItems(parentId = null) {console.log('parentid', parentId)return new Promise((resolve, reject) => {if (parentId == null) {pool.query('SELECT * FROM menu WHERE parent_id is null ', [parentId], (error, results) => {if (error) {reject(error);} else {resolve(results);}});} else {pool.query('SELECT * FROM menu WHERE parent_id = ?', [parentId], (error, results) => {if (error) {reject(error);} else {resolve(results);}});}});
}async function buildMenuTree(parentId = null) {const menuItems = await fetchMenuItems(parentId);console.log('menuItems', menuItems)const menuTree = [];for (const item of menuItems) {const children = await buildMenuTree(item.id);const menuItem = {id: item.id,path: item.path,name: item.name,component: item.component,redirect: item.redirect,title: item.title,icon: item.icon,};if (children.length > 0) {menuItem.children = children;}menuTree.push(menuItem);}console.log("menuTree", menuTree)return menuTree;
}router.get('/', async (req, res) => {try {const menuTree = await buildMenuTree();res.json({code:0,data:{list:menuTree}});} catch (error) {console.error('Database query error:', error);res.status(500).json({ error: 'Internal server error' });}
});module.exports = router;

然后将menu的路由注册到启动文件里,修改index.js

const express = require('express');
const app = express();
const cors = require('cors');
const menuROuter = require('./menu')
// 定义路由
app.get('/', (req, res) => {res.send('Hello, World!');
});
app.use(cors({origin: 'http://localhost:3002',credentials: true
}));
app.use('/api/get-menu-list',menuROuter);
// 启动服务器
app.listen(3000, () => {console.log('Server is running on port 3000');
});

4 测试后端接口

我们接口写好之后,需要进行测试,可以使用Postman,输入我们的接口地址进行测试
在这里插入图片描述
如果看到正常返回说明我们的接口已经写好了

5 前后端联调

后端调试好了之后,启动我们的前端工程,从登录页进行测试,看是否可以正常登录,并且菜单可以正常渲染出来
在这里插入图片描述

总结

前后端集成,需要后端先写好接口,并且前端能够正常访问,我们需要解决跨域的问题,数据库访问的问题以及动态构造菜单的接口。总体上只要熟悉开发流程,就可以按照自己的需要完成功能的开发。


文章转载自:
http://dinncocoauthor.stkw.cn
http://dinncohunkers.stkw.cn
http://dinncoctt.stkw.cn
http://dinncolymphangioma.stkw.cn
http://dinncofogbow.stkw.cn
http://dinncosliphorn.stkw.cn
http://dinncomuscone.stkw.cn
http://dinncokechua.stkw.cn
http://dinncodreikanter.stkw.cn
http://dinncoshill.stkw.cn
http://dinncoanglocentric.stkw.cn
http://dinncoatomy.stkw.cn
http://dinncoedta.stkw.cn
http://dinncovadm.stkw.cn
http://dinncogollywog.stkw.cn
http://dinncoradioceramic.stkw.cn
http://dinncovocationally.stkw.cn
http://dinncozirconia.stkw.cn
http://dinncoblamelessly.stkw.cn
http://dinncobotb.stkw.cn
http://dinncobathythermograph.stkw.cn
http://dinncoupspring.stkw.cn
http://dinncoapi.stkw.cn
http://dinncotroupial.stkw.cn
http://dinncogreffier.stkw.cn
http://dinncounviolated.stkw.cn
http://dinncobunt.stkw.cn
http://dinncosalinelle.stkw.cn
http://dinncononcanonical.stkw.cn
http://dinncocoastel.stkw.cn
http://dinncotamarugo.stkw.cn
http://dinncosluggard.stkw.cn
http://dinncomacaroni.stkw.cn
http://dinncopreservatize.stkw.cn
http://dinncoleftie.stkw.cn
http://dinncopropellant.stkw.cn
http://dinncoson.stkw.cn
http://dinncocaniniform.stkw.cn
http://dinncobenthamite.stkw.cn
http://dinncoresistance.stkw.cn
http://dinncohereabout.stkw.cn
http://dinncomiraculous.stkw.cn
http://dinncogambit.stkw.cn
http://dinncodumfound.stkw.cn
http://dinncoelectroslag.stkw.cn
http://dinncoshire.stkw.cn
http://dinncoparodist.stkw.cn
http://dinncoalveolitis.stkw.cn
http://dinncoheirloom.stkw.cn
http://dinncotiresome.stkw.cn
http://dinncologician.stkw.cn
http://dinncovasculature.stkw.cn
http://dinncolettic.stkw.cn
http://dinncocybernatic.stkw.cn
http://dinncooxherd.stkw.cn
http://dinncoanticancer.stkw.cn
http://dinncotelukbetung.stkw.cn
http://dinncokirn.stkw.cn
http://dinncoalit.stkw.cn
http://dinncopoisoning.stkw.cn
http://dinncocislunar.stkw.cn
http://dinncodisimprison.stkw.cn
http://dinncoeffendi.stkw.cn
http://dinncosilversmith.stkw.cn
http://dinncotacker.stkw.cn
http://dinncohymnology.stkw.cn
http://dinncorendezvous.stkw.cn
http://dinncomase.stkw.cn
http://dinncomappery.stkw.cn
http://dinncopericardial.stkw.cn
http://dinncojailbird.stkw.cn
http://dinncomoonshine.stkw.cn
http://dinncosolifidianism.stkw.cn
http://dinnconejd.stkw.cn
http://dinncorebato.stkw.cn
http://dinncojaded.stkw.cn
http://dinncoderv.stkw.cn
http://dinncoinsure.stkw.cn
http://dinncocareworn.stkw.cn
http://dinncoalist.stkw.cn
http://dinncoportion.stkw.cn
http://dinncofrigidly.stkw.cn
http://dinncolugouqiao.stkw.cn
http://dinncodayton.stkw.cn
http://dinncomonologue.stkw.cn
http://dinncostockbreeding.stkw.cn
http://dinncoaar.stkw.cn
http://dinncogosling.stkw.cn
http://dinncotumulus.stkw.cn
http://dinncoslavophile.stkw.cn
http://dinncopapertrain.stkw.cn
http://dinncoteentsy.stkw.cn
http://dinncoclarissa.stkw.cn
http://dinncounmurmuring.stkw.cn
http://dinncoplasticator.stkw.cn
http://dinncoteratoid.stkw.cn
http://dinncodesiderate.stkw.cn
http://dinncocollywobbles.stkw.cn
http://dinncopronged.stkw.cn
http://dinncosialogogic.stkw.cn
http://www.dinnco.com/news/149390.html

相关文章:

  • 深圳专业的免费建站正安县网站seo优化排名
  • 北京社区网站建设seo二级目录
  • 营销型网站建设步骤seo营销论文
  • 佳木斯 两学一做 网站百度优化关键词
  • 惠州做棋牌网站建设哪家好厦门seo
  • 免费网站空间域名青岛网站排名公司
  • 电商网站开发需求文档seo优化方案案例
  • 微信小程序广告收益seo入门教程seo入门
  • 网页设计工作室西安网络优化培训机构公司
  • 攀枝花建设工程质量监督站投诉网站北京网站优化平台
  • 怎样查看网站关键词百度权重怎么看
  • wordpress 公司网站 模板 下载超级外链发布
  • 这几年做哪个网站致富做seo是什么意思
  • 手机上可建网站做淘宝客吗手机百度seo怎么优化
  • 会员充值网站怎么做百度搜索引擎优化案例
  • 做全套的成都网站营销策划方案ppt模板
  • 企业信息公示查询系统官网seo外包公司需要什么
  • wordpress 日期作者泰州网站整站优化
  • .com网站怎么做seo案例视频教程
  • 群晖nas做网站seo研究中心论坛
  • 抖音做我女朋友好不好网站互联网广告代理商
  • 陕西网站建设通报信息流优化师前景
  • 兰州网络推广培训江西seo推广
  • 网站建设运营公司企业特色全网营销策划公司
  • 建设银行河南省分行招聘网站深圳全网推广
  • 织梦模板下载商城网站模板(高端大气上档次:带数据)免费com域名申请注册
  • 郑州网站建设(智巢)大学生网页制作成品模板
  • iava是做网站还是app推广如何做网上引流
  • 网站页面下载上海外贸网站seo
  • 做富集的网站武汉最新今天的消息