Blame view

gulp/proxy.js 1.71 KB
66ee5529f   Palak Handa   initial commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
   /*jshint unused:false */
  
  /***************
  
    This file allow to configure a proxy system plugged into BrowserSync
    in order to redirect backend requests while still serving and watching
    files from the web project
  
    IMPORTANT: The proxy is disabled by default.
  
    If you want to enable it, watch at the configuration options and finally
    change the `module.exports` at the end of the file
  
  ***************/
  
  'use strict';
  
  var httpProxy = require('http-proxy');
  var chalk = require('chalk');
  
  /*
   * Location of your backend server
   */
  var proxyTarget = 'http://server/context/';
  
  var proxy = httpProxy.createProxyServer({
    target: proxyTarget
  });
  
  proxy.on('error', function(error, req, res) {
    res.writeHead(500, {
      'Content-Type': 'text/plain'
    });
  
    console.error(chalk.red('[Proxy]'), error);
  });
  
  /*
   * The proxy middleware is an Express middleware added to BrowserSync to
   * handle backend request and proxy them to your backend.
   */
  function proxyMiddleware(req, res, next) {
    /*
     * This test is the switch of each request to determine if the request is
     * for a static file to be handled by BrowserSync or a backend request to proxy.
     *
     * The existing test is a standard check on the files extensions but it may fail
     * for your needs. If you can, you could also check on a context in the url which
     * may be more reliable but can't be generic.
     */
    if (/\.(html|css|js|png|jpg|jpeg|gif|ico|xml|rss|txt|eot|svg|ttf|woff|cur)(\?((r|v|rel|rev)=[\-\.\w]*)?)?$/.test(req.url)) {
      next();
    } else {
      proxy.web(req, res);
    }
  }
  
  /*
   * This is where you activate or not your proxy.
   *
   * The first line activate if and the second one ignored it
   */
  
  //module.exports = [proxyMiddleware];
  module.exports = [];