ToB企服应用市场:ToB评测及商务社交产业平台

标题: 【项目上线】项目上线操作步骤 [打印本页]

作者: 络腮胡菲菲    时间: 2024-8-30 12:57
标题: 【项目上线】项目上线操作步骤
目次
1. 项目打包
2. 本地服务器摆设
2.1. 具体操作步骤
2.2. 办理刷新 404 问题
2.3. 哀求无法发送问题
3. nginx 服务器摆设
3.1. nginx 简介
3.2. nginx 配置代理练习
3.3. nginx 摆设前端项目
4. 云服务器摆设


1. 项目打包



2. 本地服务器摆设

2.1. 具体操作步骤


本地服务器可以用:Java、Php、Go、Node.js 等语言编写,本教程接纳是Node.js编写服务器,端口号为:8088,且已经配置了public文件夹为静态资源。
点击下载服务器:

具体的打包命令,可以参考package.json中的scripts字段配置。



将打包天生的文件内容,放到服务器的静态资源文件夹中(上文中的public文件夹)

欣赏器访问:http://localhost:8088即可看到我们的项目,但此时会遇到两个问题:
2.2. 办理刷新 404 问题

问题分析:前端项目标路由,通常分为两种工作模式,分别为:
hash 值又称锚点,通常用于指定网页中的某个位置,比方下面的网址:
央视网_世界就在眼前,其中的#SUBD1605080062959435就是 hash 值,hash 值只在客户端(如欣赏器)中使用,是不会带给服务器的,以是使用 hash 模式时,不存在刷新 404 问题。
history 去掉了URL中的#号,可以让应用的URL看起来更美观,带来的问题就是刷新时,会将前端路由携带给后端,而后端没有对应资源的匹配,就出现了 404 问题。

办理方案一:将前端路由器工作模式改为 hash 模式 —— 不太推荐。
办理方案二:让服务器在收到未配置的GET路由时,都返回index.html即可。
方案二最终实在是把 url 中的 path,交给了前端路由去处理,具体配置如下:
  1. app.get('*',(req,res)=>{
  2.         res.sendFile(__dirname + '/public/index.html')
  3. })
复制代码
也可以借助connect-history-api-fallback中间件完成配置
  1. const history = require('connect-history-api-fallback');
  2. app.use(history());
  3. // 配置静态资源
  4. app.use(express.static(__dirname + '/public'))
复制代码
使用connect-history-api-fallback可以让配置更灵活,比如/login临时不必要作为前端路由处理,就可以按照如下方式配置
  1. app.use(history({
  2.         verbose:false,
  3.         rewrites:[
  4.                 { from: /^\/login.*$/, to: (context) => context.parsedUrl.path },
  5.         ]
  6. }))
复制代码
2.3. 哀求无法发送问题

问题分析:脱离脚手架后,就没有了代理服务器,无法转发哀求到【提供数据】的服务器。
怎样办理?—— 在 Node 服务器中借助http-proxy-middleware中间件配置代理,具体配置如下:
  1. // 引入createProxyMiddleware
  2. const { createProxyMiddleware } = require('http-proxy-middleware')
  3. // 配置代理中间件
  4. app.use('/dev', createProxyMiddleware({
  5.         target: 'http://sph-h5-api.atguigu.cn',
  6.         changeOrigin: true,
  7.         pathRewrite: {
  8.                 '^/dev': ''
  9.         }
  10. }))
复制代码
3. nginx 服务器摆设

3.1. nginx 简介

Nginx(发音为“engine-x”)是一款高性能的 HTTP 服务器和反向代理服务器,同时也是一个 IMAP/POP3/SMTP 代理服务器。Nginx 最初由 Igor Sysoev 编写,于 2004 年发布。它以其高性能、高稳固性、丰富的功能集和低系统资源消耗而闻名,重要功能有:
3.2. nginx 配置代理练习

今日头条接口地址:https://www.toutiao.com/hot-event/hot-board/?origin=toutiao_pc
直接向其发送 Ajax 哀求会有跨域问题,接下来我们借助nginx办理跨域问题
配置方式一:不过滤前缀
以办理今日头条跨域为例,不干/hot-event掉前缀配置如下
  1. location /hot-event {
  2.   # 设置代理目标
  3.   proxy_pass https://www.toutiao.com;
  4.   # 允许跨域
  5.   add_header 'Access-Control-Allow-Origin' '*';
  6.   add_header 'Access-Control-Allow-Methods' '*';
  7.   add_header 'Access-Control-Allow-Headers' '*';
  8.   add_header 'Access-Control-Expose-Headers' '*';
  9. }
复制代码
前端写法
  1. axios.get('http://localhost:8099/hot-event/hot-board/?origin=toutiao_pc')
复制代码
配置方式二:过滤前缀
办理今日头条跨域,不干/hot-event掉前缀,nginx配置(8099 端口)
  1. location /dev/ {
  2.   # 设置代理目标
  3.   proxy_pass https://www.toutiao.com/;
  4.   # 允许跨域
  5.   add_header 'Access-Control-Allow-Origin' '*';
  6.   add_header 'Access-Control-Allow-Methods' '*';
  7.   add_header 'Access-Control-Allow-Headers' '*';
  8.   add_header 'Access-Control-Expose-Headers' '*';
  9. }
复制代码
前端写法
  1. axios.get('http://localhost:8099/dev/hot-event/hot-board/?origin=toutiao_pc')
复制代码
备注:使用下面配置删除上游服务器的指定响应头。
  1. proxy_hide_header Access-Control-Allow-Origin;
复制代码
3.3. nginx 摆设前端项目

整体思绪:让nginx充当两个角色,既是 静态内容服务器,又是代理服务器
  1. # 配置nginx根目录
  2. location / {
  3.   root   D:\dist;
  4.   index  index.html index.htm;
  5. }
  6. # 配置代理
  7. location /dev/ {
  8.   # 设置代理目标
  9.   proxy_pass http://sph-h5-api.atguigu.cn/;
  10. }
复制代码
  1. const request = axios.create({
  2.   baseURL:'/dev',
  3.   timeout:10000
  4. })
复制代码
  1. http://localhost:8099
复制代码
  1. # 配置nginx根目录
  2. location / {
  3.   root   D:\dist;
  4.   index  index.html index.htm;
  5.   try_files $uri $uri/ /index.html; # 解决刷新404
  6. }
  7. # 配置代理
  8. location /dev/ {
  9.   # 设置代理目标
  10.   proxy_pass http://sph-h5-api.atguigu.cn/;
  11. }
复制代码
4. 云服务器摆设

我们可以在云服务器上借助nginx完成摆设,大致流程与本地nginx摆设划一

  1. yum install nginx
复制代码

  1. # For more information on configuration, see:
  2. #   * Official English Documentation: http://nginx.org/en/docs/
  3. #   * Official Russian Documentation: http://nginx.org/ru/docs/
  4. user nginx;
  5. worker_processes auto;
  6. error_log /var/log/nginx/error.log;
  7. pid /run/nginx.pid;
  8. # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
  9. include /usr/share/nginx/modules/*.conf;
  10. events {
  11.     worker_connections 1024;
  12. }
  13. http {
  14.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  15.                       '$status $body_bytes_sent "$http_referer" '
  16.                       '"$http_user_agent" "$http_x_forwarded_for"';
  17.     access_log  /var/log/nginx/access.log  main;
  18.     sendfile            on;
  19.     tcp_nopush          on;
  20.     tcp_nodelay         on;
  21.     keepalive_timeout   65;
  22.     types_hash_max_size 2048;
  23.     include             /etc/nginx/mime.types;
  24.     default_type        application/octet-stream;
  25.     # Load modular configuration files from the /etc/nginx/conf.d directory.
  26.     # See http://nginx.org/en/docs/ngx_core_module.html#include
  27.     # for more information.
  28.     include /etc/nginx/conf.d/*.conf;
  29.     server {
  30.         listen       80 default_server;
  31.         listen       [::]:80 default_server;
  32.         server_name  _;
  33.         root         /usr/share/nginx/html;
  34.         # Load configuration files for the default server block.
  35.         include /etc/nginx/default.d/*.conf;
  36.         location / {
  37.           root   /var/sph;
  38.           index  index.html index.htm;
  39.           try_files $uri $uri/ /index.html; # 解决刷新404
  40.         }
  41.         # 配置代理
  42.         location /dev/ {
  43.           # 设置代理目标
  44.           proxy_pass http://sph-h5-api.atguigu.cn/;
  45.         }
  46.         error_page 404 /404.html;
  47.             location = /40x.html {
  48.         }
  49.         error_page 500 502 503 504 /50x.html;
  50.             location = /50x.html {
  51.         }
  52.     }
  53. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4