Skip to content

Instantly share code, notes, and snippets.

@dohoons
Last active March 19, 2018 05:31
Show Gist options
  • Save dohoons/e5ac3ac2851410341430ca8af8c5486b to your computer and use it in GitHub Desktop.
Save dohoons/e5ac3ac2851410341430ca8af8c5486b to your computer and use it in GitHub Desktop.
gulp-webserver : middleware for support post
var gulp = require('gulp'),
fs = require('fs'),
path = require('path'),
webserver = require('gulp-webserver');
gulp.task('server', function() {
var dist = './dist';
return gulp.src(dist).pipe(webserver({
host: '0.0.0.0',
port: 4000,
livereload: false,
directoryListing: {
enable: false,
path: dist
},
middleware: [
(req, res, next) => {
var filepath = path.join(path.resolve(__dirname), dist, req.url);
if (
'POSTPUTDELETE'.indexOf(req.method.toUpperCase()) > -1
&& fs.existsSync(filepath)
&& fs.statSync(filepath).isFile()
) {
res.end(fs.readFileSync(filepath));
}
next();
}
],
open: false
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment