Browser-sync: cannot support "POST" method?

Created on 30 Mar 2016  路  3Comments  路  Source: BrowserSync/browser-sync

return browserSync.init({
        server: {
            baseDir: ['.temp',
                     '.mer',
                     `${config.serve.mer}/views`,
                     `${config.serve.temp}/views`,
                     config.serve.mer,
                     config.serve.temp
            ],
            port: config.serve.port,
            https: true,
            routes: routes
        },
        ui: {
            port: 8080
        },
        notify: false,
        open: false,
    });

Server only supports GET request does not support POST request?

Most helpful comment

@shakyShane I have a dev environment SPA running with BrowserSync, and it all works fine, but I also ran into a use case where I had to have the server accept POST requests, and it failed with a Cannot POST /...url message.

The reason I need this to work, is that I am integrating with an online payment system, and that system will issue a POST request back to my SPA url. I cannot change that to a GET request. So, in order to test, I would like my dev environment server to accept such POST requests and serve the SPA index.html file.

I have resorted to using the following simple middleware to ensure both POST requests are converted to GET requests and everything is sent to the SPA middleware:

/**
 * Static middleware
 */
let staticMiddleware = serveStatic(path.resolve(BASE_DIR));

/**
 * SPA middleware
 */
let spaMiddleware = function(req, res, next) {
  req.url = '/index.html';
  req.method = 'GET';
  next();
};

/**
 * Initialize browser sync
 */
browserSync.init({
  port: PORT,
  server: {
    baseDir: BASE_DIR,
    middleware: [
      staticMiddleware,
      spaMiddleware,
    ],
  },
});

Hope this helps someone. However, it would be nice if browserSync could have some kind of option or setting to make this easier.

All 3 comments

Thanks for the report :)

in what way does it not support POST requests? Please create a small sample project that highlights your use-case and I can look into it & re-open this issue :)

@shakyShane I have a dev environment SPA running with BrowserSync, and it all works fine, but I also ran into a use case where I had to have the server accept POST requests, and it failed with a Cannot POST /...url message.

The reason I need this to work, is that I am integrating with an online payment system, and that system will issue a POST request back to my SPA url. I cannot change that to a GET request. So, in order to test, I would like my dev environment server to accept such POST requests and serve the SPA index.html file.

I have resorted to using the following simple middleware to ensure both POST requests are converted to GET requests and everything is sent to the SPA middleware:

/**
 * Static middleware
 */
let staticMiddleware = serveStatic(path.resolve(BASE_DIR));

/**
 * SPA middleware
 */
let spaMiddleware = function(req, res, next) {
  req.url = '/index.html';
  req.method = 'GET';
  next();
};

/**
 * Initialize browser sync
 */
browserSync.init({
  port: PORT,
  server: {
    baseDir: BASE_DIR,
    middleware: [
      staticMiddleware,
      spaMiddleware,
    ],
  },
});

Hope this helps someone. However, it would be nice if browserSync could have some kind of option or setting to make this easier.

@shakyShane Any update on this? I am trying to be able to accept other HTTP verbs besides GET

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danielverejan picture danielverejan  路  3Comments

npearson72 picture npearson72  路  3Comments

zewa666 picture zewa666  路  3Comments

Zver64 picture Zver64  路  3Comments

hgl picture hgl  路  3Comments