使用 grunt 服务器,如何将所有请求重定向到 root url? [英] Using grunt server, how can I redirect all requests to root url?

查看:27
本文介绍了使用 grunt 服务器,如何将所有请求重定向到 root url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建我的第一个 Angular.js 应用程序,我正在使用 Yeoman.

I am building my first Angular.js application and I'm using Yeoman.

Yeoman 使用 Grunt 允许您使用命令grunt server"运行 node.js 连接服务器.

Yeoman uses Grunt to allow you to run a node.js connect server with the command 'grunt server'.

我正在 html5 模式下运行我的 angular 应用程序.根据 angular 文档,这需要修改服务器以将所有请求重定向到应用程序的根目录 (index.html),因为 angular 应用程序是单页 ajax 应用程序.

I'm running my angular application in html5 mode. According to the angular docs, this requires a modification of the server to redirect all requests to the root of the application (index.html), since angular apps are single page ajax applications.

使用 [html5] 模式需要在服务器端重写 URL,基本上你必须重写所有指向应用程序入口点的链接(例如 index.html)"

"Using [html5] mode requires URL rewriting on server side, basically you have to rewrite all your links to entry point of your application (e.g. index.html)"

我试图解决的问题在 这个问题.

The problem that I'm trying to solve is detailed in this question.

如何修改我的 grunt 服务器以将所有页面请求重定向到 index.html 页面?

How can I modify my grunt server to redirect all page requests to the index.html page?

推荐答案

首先,使用命令行,导航到包含 gruntfile 的目录.

First, using your command line, navigate to your directory with your gruntfile.

在 CLI 中输入:

npm install --save-dev connect-modrewrite

在你的 grunt 文件的顶部放这个:

At the top of your grunt file put this:

var modRewrite = require('connect-modrewrite');

现在是下一部分,您只需要将 modRewrite 添加到您的连接中:

Now the next part, you only want to add modRewrite into your connect:

modRewrite(['!\\.html|\\.js|\\.svg|\\.css|\\.png$ /index.html [L]']),

这是我的连接"在 Gruntfile.js 中的样子的示例.您无需担心我的 lrSnippet 和 ssIncludes.您需要做的主要事情是获取 modRewrite.

Here is a example of what my "connect" looks like inside my Gruntfile.js. You don't need to worry about my lrSnippet and my ssIncludes. The main thing you need is to just get the modRewrite in.

        connect: {
        options: {
            port: 9000,
            // Change this to '0.0.0.0' to access the server from outside.
            hostname: '0.0.0.0',
        },
        livereload: {
            options: {
                middleware: function (connect) {
                return [
                        modRewrite(['!\\.html|\\.js|\\.svg|\\.css|\\.png$ /index.html [L]']),
                        lrSnippet,
                        ssInclude(yeomanConfig.app),
                        mountFolder(connect, '.tmp'),
                        mountFolder(connect, yeomanConfig.app)
                        ];
                }
            }
        },
        test: {
            options: {
                middleware: function (connect) {
                    return [
                    mountFolder(connect, '.tmp'),
                    mountFolder(connect, 'test')
                    ];
                }
            }
        },
        dist: {
            options: {
                middleware: function (connect) {
                    return [
                    mountFolder(connect, yeomanConfig.dist)
                    ];
                }
            }
        }
    },

这篇关于使用 grunt 服务器,如何将所有请求重定向到 root url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆