前端VSCode layui项目+后端IDEA Springboot项目实现前后端分离

本文介绍了如何在Vscode中运行layui前端项目,IDEA中运行Springboot后端项目,以及通过Nginx实现前后端数据交互。前端使用Debugger for Chrome插件和live-server,后端设置项目端口号为8082。通过Nginx的反向代理,实现了前端访问http://localhost:8080/api时,实际请求被代理到http://localhost:8082,确保了前后端的正常通信。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

软件:前端Vscode运行layui项目,后端IDEA运行springboot项目

1.前端

1、 安装插件Debugger for Chrome
在这里插入图片描述

2、配置文件,在launch.json文件添加配置信息
在这里插入图片描述
在这里插入图片描述

3、选择调试方式为本机chrome调试
在这里插入图片描述

4、在终端输入npm install -g live-server
在这里插入图片描述
5、在终端输入live-server命令
输入live-server遇到错误
在这里插入图片描述

解决:windows powershell 管理员身份打开,依次输入以下命令

set-ExecutionPolicy RemoteSigned

A

get-ExecutionPolicy

如下图,显示RemoteSigned
在这里插入图片描述
再次在layui程序终端运行live-server命令不再报错

2. 后端

springboot项目的application.properties文件设置项目端口号为8082

#项目端口号
server.port=8082

3. 前后端数据交互

参考了https://www.freesion.com/article/199570292/

1、配置nginx反向代理
在官网http://nginx.org/en/download.html下载nginx
解压后双击nginx.exe,在cmd中定位到nginx目录下运行start nginx命令
打开http:localhost:8080出现页面
在这里插入图片描述
表示启动成功

2、引入自定义配置文件
在nginx.conf文件的http中添加

#引入自定义配置文件
include reverse-procy.conf;

在这里插入图片描述
3、在nginx安装目录中新建reverse-procy.conf文件
在这里插入图片描述
在reverse-procy.conf文件编写以下内容

server {
    listen 8080;
    server_name localhost;    # 这是外网访问进来时的连接地址
    location /{
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:8081;
    }
	location /api{
		rewrite ^.+api/?(.*)$ /$1 break;
		include uwsgi_params;
		proxy_pass http://localhost:8082;
	}
    
}

在cmd中用nginx -s reload命令重新加载nginx配置即可

4、为了实现前后端数据交互,需要用到Nginx的正向代理和反向代理
前端静态页面为 http://localhost:8081/
后端服务端口为 http://localhost:8082/

用户访问http://localhost:8080/login.html前端页面
登录页面使用ajax调取后端接口`

http://localhost:8082/user/login`

用到的就是nginx的反向代理

http://localhost:8080/api/user/login

映射为

http://localhost:8082/user/login

配置项让Nginx 监听了 localhost的8080端口,当用户访问 http://localhost:8080
的时候,Nginx会正向代理到 http://localhost:8081 端口(即前端layui框架的页面) 然后当用户访问
http://localhost:8080/api 的时候,nginx会反向代理到 http://localhost:8082

所以前端layui项目的url需要命名为/api开头的格式,在VSCode的终端使用live-server命令运行layui项目
在这里插入图片描述
之前默认的live-server项目端口是8080,需要在C:\Users\xuan\AppData\Roaming\npm\node_modules\live-server\index.js文件中将默认的8080端口改为8081端口
在这里插入图片描述

5、使用nigx反向代理后分别运行前后端项目
前端运行结果
在这里插入图片描述
后端运行结果
在这里插入图片描述

6、访问http://localhost:8080/login.html能够完成登录功能表示项目正常运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值