question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using 'browser-sync' and 'connect-modrewrite'

See original GitHub issue

I am working on a angular.js app. My problem is when working locally, when I refresh my URL i get Cannot GET / when a page is refreshed other than the index.html etc.

My problem is solved on my production server using:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(.*) /index.html [NC,L]
</IfModule>

However I’d like a solution which works with gulp, locally. I have read you can use ‘connect-modrewrite’ like so:

gulp.task('browser-sync', function() {
browserSync({
    server: {
        baseDir: outputDir,
    middleware: function() {
        return [
            modRewrite([
                '^/api/v1/(.*)$ http://localhost:3000/api/v1/$1 [P]'
            ])
        ];
    }
    }
});
});

The above doesn’t work, so I assume its incorrect. Am I way off?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
FadelChafaicommented, Nov 22, 2014

Hi,

You can use : connect-modrewrite

npm install --save-dev connect-modrewrite

and in your gulpfile.js :

..........................
var modRewrite  = require('connect-modrewrite');
..........................
gulp.task('serve', function () {
    browserSync.init(null, {
        server: {
            baseDir: ['.tmp', 'app'],  
            middleware: [
                modRewrite([
                    '!\\.\\w+$ /index.html [L]'
                ])
            ]
        }
    });

    gulp.watch(['app/**/*.html'], reload);
    gulp.watch(['app/styles/**/*.{scss,css}'], ['styles', reload]);
    gulp.watch(['app/scripts/**/*.js'], ['jshint']);
    gulp.watch(['app/images/**/*'], reload);
});

....


0reactions
matinfocommented, Jun 11, 2016

@FadelChafai Thanks worked perfectly locally with AngularJS ui-Router with $locationProvider.html5Mode(true);

Read more comments on GitHub >

github_iconTop Results From Across the Web

Browsersync options
Browsersync options. These are all the options that you can configure when using Browsersync. Create a single object and pass it as the...
Read more >
Browsersync Command Line Usage
First, either install Browsersync globally, or locally to your project (if you're using npm scripts) and then run one of the following commands...
Read more >
Browsersync + Gulp.js
Browsersync makes your browser testing workflow faster by synchronising URLs, interactions and code changes across multiple devices.
Read more >
Browsersync - Time-saving synchronised browser testing
Browsersync makes your browser testing workflow faster by synchronising URLs, interactions and code changes across multiple devices.
Read more >
Browsersync API
Our API is incredibly simple & powerful. You can use it to create your own tiny node program for local development or integrate...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found