const browserSync = require('browser-sync');
const reload = browserSync.reload;
const browserSyncConfig = require('./browser-sync-config');
gulp.task('browser-sync', function(){
browserSync(browserSyncConfig.server.browser);
});
var fs = require('fs');
var path = require('path');
var url = require('url');
const routes = {
"/systemfile.js": "/systemfile.js"
};
const CONTENT_TYPES = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css'
};
function readFile(path){
return new Promise(function(resolve, reject){
fs.readFile(path, 'utf8', function(error, data){
if(error){
reject(error);
}else{
resolve(data);
}
});
});
}
const SERVER = {
browser: {
online: false,
port: 8080,
open: false,
injectChanges: false,
proxy: {
target: "http://localhost:3000",
middleware: function(req, res, next){
var route = routes[req.url] || req.url;
var type = CONTENT_TYPES[path.parse(route).ext];
if(route && type){
readFile(global.process.cwd() + route)
.then(function(data){
console.log(route, type, req.url);
res.writeHead(200, { "Content-Type": type });
res.write(data.toString('utf8'));
res.end();
})
.catch(function(error){
console.log('404 || redirect', error, req.url, route, type);
res.writeHead(404);
res.end();
});
}else{
next();
}
}
},
serveStatic: ["/public/css/"]
},
reload: {
stream: true
}
};
module.exports = {
server: SERVER
};
.pipe(reload(browserSyncConfig.server.reload))