使用Express和Vue进行路由

时间:2020-07-10 15:24:21

标签: javascript express vue.js vue-router express-router

我正在创建一个带有索引“ /”和404的简单页面,以捕获错误“ / 404”。

我的快速应用程序中包含以下内容:

// Entry Point
app.use("/", express.static(resolve(__dirname, "client/dist")));

然后这是我在Vue中的路由器

const routes = [
    {
        path: '/',
        name: 'Home',
        component: Home,
    },
    {
        path: '/404',
        name: 'PageNotFoumd',
        component: () => import('../components/modules/PageNotFound'),
    },
    {
        path: '*',
        redirect: '/404',
    },
];

const router = new VueRouter({
    mode: 'history',
    base: '/',
    routes,
});

因此,如果我启动客户端应用程序,然后转到“ / 13eo31be”,它将使用Vue的路由器将我重定向到“ / 404”。但是,如果我构建Vue应用并通过服务器运行它(它通常在网站上运行的方式),则结果如下:

  • '/'=>'/'
  • '/ 404'=>无法获取/ 404-应该转到'/ 404'
  • '/ 2323f2f'=>无法获取/ 2323f2f-应该转到'/ 404'

我如何允许Express将重定向的内容传递给Vue路由器?

1 个答案:

答案 0 :(得分:1)

没关系,这是一个简单的问题。

我只需要将'/ 404'传递给与'/'相同的索引文件。

app.use("/404", express.static(config.path));