blob: b76c38e37355732ed2c2770d80fe6612f70c2e80 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*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 = [];