【问题标题】:Express Server routing configuration for Vue AppVue App 的 Express Server 路由配置
【发布时间】:2020-08-14 14:17:19
【问题描述】:

我想使用 Express 服务器(在生产环境中)为我的 Vue 应用程序提供服务。我认为我当前的配置有问题(缺少?),因为它没有像应有的那样运行应用程序。

这是我的配置:

simple-tasks-organizer-frontend/package.json

{
   "scripts": {
    "start-dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start-prod": "node server.js"
  },
}

simple-tasks-organizer-frontend/server.js


const history = require('connect-history-api-fallback');
var express = require('express');

var PORT = 8080;
var app = express();
app.use(history());
app.listen(PORT);

simple-tasks-organizer-frontend/src/router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import Signin from '@/components/Signin.vue'
import Signup from '@/components/Signup.vue'
import Records from '@/components/tasks/Tasks.vue'
import Automations from '@/components/automations/Automations.vue'

Vue.use(Router)

export default new Router({
  mode: 'history',
  routes: [
    {
      path: '/tasks',
      name: 'Tasks',
      component: Records
    },
    {
      path: '/automations',
      name: 'Automations',
      component: Automations
    },
    {
      path: '/',
      name: 'Signin',
      component: Signin
    },
    {
      path: '/signup',
      name: 'Signup',
      component: Signup
    }
  ]
})

当我输入 http://localhost:8080/ 时,我收到以下错误消息:Cannot GET /

希望大家能帮我解决这个问题!

提前致谢!

【问题讨论】:

    标签: javascript node.js vue.js


    【解决方案1】:

    事实上,您不必使用 express 服务器为您的 Vue 应用程序提供服务。

    Vue 路由与 Express.js 路由无关。

    Vue 应用,一旦构建就可以部署到任何静态服务器(apache、nginx、S3 等)

    如果您正在寻找具有服务器端渲染的一体化解决方案,您应该考虑使用nuxt.js

    如果您仍需要使用 express 服务您的应用,请查看express-serve-static-core。请记住,express 应该为您构建的应用版本提供服务。

    【讨论】:

    • 感谢 Ivan 的快速响应!我是这个主题的绝对新手,所以也许你可以为我提供更多信息,我如何将 nginx 用于 vue 应用程序?我的后端 API 已经在 nginx 和 puma 上运行。为了在 nginx 上运行 Vue 应用程序,我必须执行哪些步骤?
    • 只需将 nginx 设置为提供静态内容,就像您设置提供任何其他静态内容(图像/html/等)的方式一样。构建您的 vue 应用程序(npm run build)并将输出的内容(默认为 /dist 文件夹)放置到您的 nginx 服务器文件所在的位置。
    • 太棒了!我会那样做的!谢谢伊万!!
    猜你喜欢
    • 2018-11-16
    • 1970-01-01
    • 2014-06-10
    • 2023-03-22
    • 2019-07-20
    • 2020-10-31
    • 1970-01-01
    • 2015-05-27
    • 2016-02-14
    相关资源
    最近更新 更多