Skip to content

Instantly share code, notes, and snippets.

@wescosta
Last active February 8, 2017 21:43
Show Gist options
  • Save wescosta/c3f649ba1cb4a752a806fa2b4530864d to your computer and use it in GitHub Desktop.
Save wescosta/c3f649ba1cb4a752a806fa2b4530864d to your computer and use it in GitHub Desktop.
/*
|--------------------------------------------------------------------------
| Browser-sync config file
|--------------------------------------------------------------------------
|
| For up-to-date information about the options:
| http://www.browsersync.io/docs/options/
|
| There are more options than you see here, these are just the ones that are
| set internally. See the website for more info.
|
|
*/
var proxy = require('http-proxy-middleware'),
fake = require('./src/json/fake-data/bs-faker.js')
module.exports = {
"middleware": [
proxy('/rest', {target: 'http://some-server'})
].concat(fake.json('./src/json/fake-data'))
};
/*!
Little utility that exposes fake data in the form of midllewares,
which can be used within a local server, such as browser sync,
to fake backend responses locally.
The json files should set up a url and a response
as in the examples bellow
bla.json
{
"url": "/bla"
"response": "blabla"
}
fake-object.json
{
"url": "/fake-object"
"response": {
"some": "object"
}
}
fake-array.json
{
"url": "/fake-array"
"response": [1, 2, 3, 'here', 'we', 'go!']
}
*/
var glob = require('glob'),
path = require('path')
module.exports.json = function(folder){
return glob
.sync('**/*.json', {
absolute: true,
cwd: path.resolve(folder || '.')
})
.map(function(file){
return path.resolve(folder, file)
})
.map(require)
.map(function(config){
return {
route: config.url,
handle: function (req, res, next) {
var data = JSON.stringify(config.response),
status = data.fakeFaluire ? 500 : 200
res.writeHead(status, {'Content-Type': 'text/plain'})
res.end(data)
// next() commented out since browser-sync was throwing errors
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment