成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

vue、react等單頁(yè)面項(xiàng)目應(yīng)該這樣子部署到服務(wù)器

sumory / 2412人閱讀

摘要:服務(wù)端渲染等服務(wù)端渲染框架構(gòu)建的項(xiàng)目部署到服務(wù)器,并用守護(hù)程序最近好多伙伴說,我用做的項(xiàng)目本地是可以的,但部署到服務(wù)器遇到好多問題資源找不到,直接訪問頁(yè)面空白,刷新當(dāng)前路由。。。

服務(wù)端渲染:next.js、nuxt.js等服務(wù)端渲染框架構(gòu)建的項(xiàng)目部署到服務(wù)器,并用PM2守護(hù)程序

最近好多伙伴說,我用vue做的項(xiàng)目本地是可以的,但部署到服務(wù)器遇到好多問題:資源找不到直接訪問index.html頁(yè)面空白,刷新當(dāng)前路由404。。。用react做的項(xiàng)目也同樣遇到類似問題?,F(xiàn)在我們一起討論下單頁(yè)面如何部署到服務(wù)器?

由于前端路由緣故,單頁(yè)面應(yīng)用應(yīng)該放到nginx或者apache、tomcat等web代理服務(wù)器中,千萬(wàn)不要直接訪問index.html,同時(shí)要根據(jù)自己服務(wù)器的項(xiàng)目路徑更改react或vue的路由地址。

如果說項(xiàng)目是直接跟在域名后面的,比如:http://www.sosout.com ,根路由就是 "/"。
如果說項(xiàng)目是直接跟在域名后面的一個(gè)子目錄中的,比如:http://www.sosout.com/children ,根路由就是 "/children ",不能直接訪問index.html。

以配置Nginx為例,配置過程大致如下:(假設(shè):
1、項(xiàng)目文件目錄: /mnt/html/spa(spa目錄下的文件就是執(zhí)行了npm run dist 后生成的dist目錄下的文件)
2、訪問域名:spa.sosout.com
進(jìn)入nginx.conf新增如下配置:

server {
    listen 80;
    server_name  spa.sosout.com;
    root /mnt/html/spa;
    index index.html;
    location ~ ^/favicon.ico$ {
        root /mnt/html/spa;
    }

    location / {
        try_files $uri $uri/ /index.html;
        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_set_header   X-Forwarded-Proto  $scheme;
    }
    access_log  /mnt/logs/nginx/access.log  main;
}

注意事項(xiàng):
1、配置域名的話,需要80端口,成功后,只要訪問域名即可訪問的項(xiàng)目
2、如果你使用了react-router的 browserHistory 模式或 vue-router的 history 模式,在nginx配置還需要重寫路由:

server {
    listen 80;
    server_name  spa.sosout.com;
    root /mnt/html/spa;
    index index.html;
    location ~ ^/favicon.ico$ {
        root /mnt/html/spa;
    }

    location / {
        try_files $uri $uri/ @fallback;
        index index.html;
        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_set_header   X-Forwarded-Proto  $scheme;
    }
    location @fallback {
        rewrite ^.*$ /index.html break;
    }
    access_log  /mnt/logs/nginx/access.log  main;
}

為什么要重寫路由?因?yàn)槲覀兊捻?xiàng)目只有一個(gè)根入口,當(dāng)輸入類似/home的url時(shí),如果找不到對(duì)應(yīng)的頁(yè)面,nginx會(huì)嘗試加載index.html,這是通過react-router或vue-router就能正確的匹配我們輸入的/home路由,從而顯示正確的home頁(yè)面,如果browserHistory模式或history模式的項(xiàng)目沒有配置上述內(nèi)容,會(huì)出現(xiàn)404的情況。

簡(jiǎn)單舉兩個(gè)例子,一個(gè)vue項(xiàng)目一個(gè)react項(xiàng)目:

vue項(xiàng)目:

域名:http://tb.sosout.com

import App from "../App"

// 首頁(yè)
const home = r => require.ensure([], () => r(require("../page/home/index")), "home")

// 物流
const logistics = r => require.ensure([], () => r(require("../page/logistics/index")), "logistics")

// 購(gòu)物車
const cart = r => require.ensure([], () => r(require("../page/cart/index")), "cart")

// 我的
const profile = r => require.ensure([], () => r(require("../page/profile/index")), "profile")

// 登錄界面
const login = r => require.ensure([], () => r(require("../page/user/login")), "login")

export default [{
  path: "/",
  component: App, // 頂層路由,對(duì)應(yīng)index.html
  children: [{
    path: "/home", // 首頁(yè)
    component: home
  }, {
    path: "/logistics", // 物流
    component: logistics,
    meta: {
      login: true
    }
  }, {
    path: "/cart", // 購(gòu)物車
    component: cart,
    meta: {
      login: true
    }
  }, {
    path: "/profile", // 我的
    component: profile
  }, {
    path: "/login", // 登錄界面
    component: login
  }, {
    path: "*",
    redirect: "/home"
  }]
}]

############
# 其他配置
############

http {
    ############
    # 其他配置
    ############
    server {
        listen 80;
        server_name  tb.sosout.com;
        root /mnt/html/tb;
        index index.html;
        location ~ ^/favicon.ico$ {
            root /mnt/html/tb;
        }
    
        location / {
            try_files $uri $uri/ @fallback;
            index index.html;
            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_set_header   X-Forwarded-Proto  $scheme;
        }
        location @fallback {
            rewrite ^.*$ /index.html break;
        }
        access_log  /mnt/logs/nginx/access.log  main;
    }
    ############
    # 其他配置
    ############   
}

react項(xiàng)目:

域名:http://antd.sosout.com

/**
* 疑惑一:
* React createClass 和 extends React.Component 有什么區(qū)別?
* 之前寫法:
* let app = React.createClass({
*      getInitialState: function(){
*        // some thing
*      }
*  })
* ES6寫法(通過es6類的繼承實(shí)現(xiàn)時(shí)state的初始化要在constructor中聲明):
* class exampleComponent extends React.Component {
*    constructor(props) {
*        super(props);
*        this.state = {example: "example"}
*    }
* }
*/

import React, {Component, PropTypes} from "react"; // react核心
import { Router, Route, Redirect, IndexRoute, browserHistory, hashHistory } from "react-router"; // 創(chuàng)建route所需
import Config from "../config/index";
import layout from "../component/layout/layout"; // 布局界面

import login from "../containers/login/login"; // 登錄界面

/**
 * (路由根目錄組件,顯示當(dāng)前符合條件的組件)
 * 
 * @class Roots
 * @extends {Component}
 */
class Roots extends Component {
    render() {
        // 這個(gè)組件是一個(gè)包裹組件,所有的路由跳轉(zhuǎn)的頁(yè)面都會(huì)以this.props.children的形式加載到本組件下
        return (
            
{this.props.children}
); } } // const history = process.env.NODE_ENV !== "production" ? browserHistory : hashHistory; // 快速入門 const home = (location, cb) => { require.ensure([], require => { cb(null, require("../containers/home/homeIndex").default) }, "home"); } // 百度圖表-折線圖 const chartLine = (location, cb) => { require.ensure([], require => { cb(null, require("../containers/charts/lines").default) }, "chartLine"); } // 基礎(chǔ)組件-按鈕 const button = (location, cb) => { require.ensure([], require => { cb(null, require("../containers/general/buttonIndex").default) }, "button"); } // 基礎(chǔ)組件-圖標(biāo) const icon = (location, cb) => { require.ensure([], require => { cb(null, require("../containers/general/iconIndex").default) }, "icon"); } // 用戶管理 const user = (location, cb) => { require.ensure([], require => { cb(null, require("../containers/user/userIndex").default) }, "user"); } // 系統(tǒng)設(shè)置 const setting = (location, cb) => { require.ensure([], require => { cb(null, require("../containers/setting/settingIndex").default) }, "setting"); } // 廣告管理 const adver = (location, cb) => { require.ensure([], require => { cb(null, require("../containers/adver/adverIndex").default) }, "adver"); } // 組件一 const oneui = (location, cb) => { require.ensure([], require => { cb(null, require("../containers/ui/oneIndex").default) }, "oneui"); } // 組件二 const twoui = (location, cb) => { require.ensure([], require => { cb(null, require("../containers/ui/twoIndex").default) }, "twoui"); } // 登錄驗(yàn)證 const requireAuth = (nextState, replace) => { let token = (new Date()).getTime() - Config.localItem("USER_AUTHORIZATION"); if(token > 7200000) { // 模擬Token保存2個(gè)小時(shí) replace({ pathname: "/login", state: { nextPathname: nextState.location.pathname } }); } } const RouteConfig = ( // 默認(rèn)加載的組件,比如訪問www.test.com,會(huì)自動(dòng)跳轉(zhuǎn)到www.test.com/home // 所有的訪問,都跳轉(zhuǎn)到Roots // 默認(rèn)加載的組件,比如訪問www.test.com,會(huì)自動(dòng)跳轉(zhuǎn)到www.test.com/home ); export default RouteConfig;

############
# 其他配置
############

http {
    ############
    # 其他配置
    ############
    server {
        listen 80;
        server_name  antd.sosout.com;
        root /mnt/html/reactAntd;
        index index.html;
        location ~ ^/favicon.ico$ {
            root /mnt/html/reactAntd;
        }

        location / {
            try_files $uri $uri/ @router;
            index index.html;
            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_set_header   X-Forwarded-Proto  $scheme;
        }
        location @router {
            rewrite ^.*$ /index.html break;
        }
        access_log  /mnt/logs/nginx/access.log  main;
    }

    ############
    # 其他配置
    ############   
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/39756.html

相關(guān)文章

  • 前端最實(shí)用書簽(持續(xù)更新)

    摘要:前言一直混跡社區(qū)突然發(fā)現(xiàn)自己收藏了不少好文但是管理起來有點(diǎn)混亂所以將前端主流技術(shù)做了一個(gè)書簽整理不求最多最全但求最實(shí)用。 前言 一直混跡社區(qū),突然發(fā)現(xiàn)自己收藏了不少好文但是管理起來有點(diǎn)混亂; 所以將前端主流技術(shù)做了一個(gè)書簽整理,不求最多最全,但求最實(shí)用。 書簽源碼 書簽導(dǎo)入瀏覽器效果截圖showImg(https://segmentfault.com/img/bVbg41b?w=107...

    sshe 評(píng)論0 收藏0
  • vue面試

    摘要:雖然計(jì)算屬性在大多數(shù)情況下更合適,但有時(shí)也需要一個(gè)自定義的偵聽器,當(dāng)需要在數(shù)據(jù)變化時(shí)執(zhí)行異步或開銷較大的操作時(shí),通過偵聽器最有用。路由的鉤子函數(shù)首頁(yè)可以控制導(dǎo)航跳轉(zhuǎn),,等,一般用于頁(yè)面的修改。 談?wù)勀銓?duì)MVVM開發(fā)模式的理解 MVVM分為Model、View、ViewModel三者。Model 代表數(shù)據(jù)模型,數(shù)據(jù)和業(yè)務(wù)邏輯都在Model層中定義;View 代表UI視圖,負(fù)責(zé)數(shù)據(jù)的展示;...

    vspiders 評(píng)論0 收藏0
  • Vue面試中,經(jīng)常會(huì)被問的面試題/Vue知識(shí)點(diǎn)整理

    摘要:可以在該鉤子中進(jìn)一步地更改狀態(tài),不會(huì)觸發(fā)附加的重渲染過程。我工作中只用到,對(duì)和不怎么熟與的區(qū)別相同點(diǎn)都支持指令內(nèi)置指令和自定義指令都支持過濾器內(nèi)置過濾器和自定義過濾器都支持雙向數(shù)據(jù)綁定都不支持低端瀏覽器。 看看面試題,只是為了查漏補(bǔ)缺,看看自己那些方面還不懂。切記不要以為背了面試題,就萬(wàn)事大吉了,最好是理解背后的原理,這樣面試的時(shí)候才能侃侃而談。不然,稍微有水平的面試官一看就能看出,是...

    mengbo 評(píng)論0 收藏0
  • 大前端 - 收藏集 - 掘金

    摘要:是目前唯一一個(gè)支持同步調(diào)用的跨平臺(tái)年度上最多的個(gè)項(xiàng)目前端掘金年接近尾聲,在最近的幾篇文章中,會(huì)整理總結(jié)一些年度開源項(xiàng)目。 JS 全棧教程 - 前端 - 掘金本課程是基于阮一峰的 js 全棧教程的視頻版本,免費(fèi)供大家觀看... 2016 年 10 個(gè)最佳的 CodePen 作品 - 前端 - 掘金說到 CodePen,前端開發(fā)者們肯定不會(huì)陌生。如果說 Dribbble 是設(shè)計(jì)師們聚集的圣...

    honhon 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<