簡體   English   中英

如何解決 Gruntfile.js 中配置錯誤的 grunt-connect-proxy 設置導致的 404 錯誤?

[英]How do I resolve 404 error from misconfigured grunt-connect-proxy settings in Gruntfile.js?

背景:

我正在嘗試將我的 grunt 服務器實例連接到在 localhost:8080/api/ 的同一台機器上運行的 API 服務。

目前使用 grunt-connect-proxy 來實現這一點。

問題/疑問:

http://localhost:9000/api/user-profile/ Failed to load resource: the server responded with a status of 404 (Not Found)

我的配置(如下)是否有錯誤阻止/api請求重定向到 localhost:8080 的代理服務器?

我的設置(Gruntfile.js):

var proxySnippet = require('grunt-connect-proxy/lib/utils').proxyRequest;

...

    // Grunt configuration
    grunt.initConfig({

        // Project settings
        someApp: appConfig,

        // The grunt server settings
        connect: {
            options: {
                port: 9000,
                hostname: 'localhost',
                livereload: 35729
            },
            server: {
                proxies: [
                    {
                        context: '/api',
                        host: 'localhost',
                        port: 8080,
                        changeOrigin: true
                    }
                ]
            },
            livereload: {
                options: {
                    open: true,
                    middleware: function (connect) {
                        return [
                            proxySnippet,
                            connect.static('.tmp'),
                            connect().use(
                                '/bower_components',
                                connect.static('./bower_components')
                            ),
                            connect.static(appConfig.app),
                        ];
                    }
                }
            },
            main: {
                options: {
                    open: true,
                    base: '<%= homer.main %>'
                }
            }
        }

...

    grunt.registerTask('live', [
        'clean:server',
        'copy:styles',
        'configureProxies',
        'connect:livereload',
        'watch'
    ]);

    grunt.registerTask('server', [
        'build',
        'connect:main:keepalive'
    ]);

在 configureProxies 任務中指定連接目標(在本例中為“服務器”)。

grunt.registerTask('live', function (target) {
    grunt.task.run([
    'clean:server',
    'copy:styles',
    'configureProxies:server',
    'connect:livereload',
    'watch'
    ]);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM