溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在一臺vps上面部署vue+mongodb+express項目

發(fā)布時間:2021-02-08 09:44:44 來源:億速云 閱讀:148 作者:小新 欄目:web開發(fā)

這篇文章給大家分享的是有關(guān)怎么在一臺vps上面部署vue+mongodb+express項目的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

項目: vue + express + mongodb

項目前后分離部署在一臺服務(wù)器上面

  • express端口:3000

  • mongodb端口:27017

  • vue端口:本地是8080 服務(wù)端是:80

本地開發(fā)配置

本地開發(fā)基于vue cli 端口是 8080如果請求api的時候在前綴加上localhost:3000會提示跨域問題,我們可以使用下面方式來解決這個問題

在vue項目路徑找到這個文件 /vue-item/config/index.js 找到這行代碼:

proxyTable: {}

添加如下配置

demo:

proxyTable: {
    '/v1/**':{
    target: 'http://localhost:3000/',
    pathRewrite: {
     '^/v1': '/'
    }
   }
  }

v1 是我給api自動添加的前綴

這個前綴可以使用 axios 配置添加

在main.js 主入口文件添加

如下

import apiConfig from '../config/api.config'
// import axios
import Axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, Axios)
// Axios.defaults.baseURL = apiConfig.baseUrl;
Axios.defaults.baseURL = 'v1/' 這樣也ok的

api.config

判斷是開發(fā)模式還是本地模式,其實不需要這么麻煩 直接

const isProdMode = Object.is(process.env.NODE_ENV, 'production')

module.exports = {
 baseUrl: isProdMode ? 'api.shudong.wang/v1/' : 'v1/'
}

如果把axios 配置了自動前綴

每次訪問的時候

 data(){
  return {
   articleList:Object
  }
 },
 mounted: function(){
  this.getArticleList()
 },
 methods:{
  getArticleList(){
   console.log(111111111)
     this.$http.get("/article/list") // this.$http axios使用的一種方式
     .then((response)=>{
       console.log(response.data)
       let res = response.data;
       this.articleList = res.data;

     })
     .catch((error) =>{
      console.log(error)
     })
  }
 },

上面請求的例子中相當(dāng)于訪問: localhost:8080/v1/article/list

這樣就可以解決跨域問題

其實最終訪問的是 localhost:3000/article/list express的api

這個v1只是api版本的標(biāo)識,如果想帶著,并且api是可以v1版本方式訪問的,把代理的路徑重新規(guī)則去掉就可以

操作如下:

proxyTable: {
    '/v1/**':{
    target: 'http://localhost:3000/',
    //pathRewrite: { //這個規(guī)則去掉
    // '^/v1': '/'
    //}
   },
   '/goods/*':{
    target:'http://localhost:3000'
   },
   '/users/**':{
    target:'http://localhost:3000'
   }
  }

服務(wù)端部署

本地可以使用proxyTable 解決跨域問題,那么服務(wù)端怎么解決跨域問題呢?

answer:使用nginx反向代理

nginx配置: 仔細(xì)分析一下看看是否適合自己的業(yè)務(wù)場景

server
  {
    listen 80;
    #listen [::]:80;
    server_name zhenfan.shudong.wang ; # 你的域名不需要加http 
    index index.html index.htm index.php default.html default.htm default.php;
    root /home/wwwroot/zhenfan/dist;

    include none.conf;
    #error_page  404  /404.html;

    # Deny access to PHP files in specific directory
    #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

    include enable-php.conf;

    location /v1 {
      proxy_pass http://127.0.0.1:3000/; # 當(dāng)訪問v1的時候默認(rèn)轉(zhuǎn)發(fā)到 3000端口
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
      expires   30d;
    }

    location ~ .*\.(js|css)?$
    {
      expires   12h;
    }

    location ~ /.well-known {
      allow all;
    }

    location ~ /\.
    {
      deny all;
    }

    access_log off;
  }

關(guān)于express鏈接mongodb可以直接填寫端口號,不存在跨域問題,直接 127.0.0.1:27017就ok,

感謝各位的閱讀!關(guān)于“怎么在一臺vps上面部署vue+mongodb+express項目”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI