Skip to content

Instantly share code, notes, and snippets.

@webuniverseio
Forked from sogko/app.js
Created January 24, 2017 13:17
Show Gist options
  • Save webuniverseio/264984ce44dcbe343dcb9b6c87d95fce to your computer and use it in GitHub Desktop.
Save webuniverseio/264984ce44dcbe343dcb9b6c87d95fce to your computer and use it in GitHub Desktop.
gulp + expressjs + nodemon + browser-sync
'use strict';
// simple express server
var express = require('express');
var app = express();
var router = express.Router();
app.use(express.static('public'));
app.get('/', function(req, res) {
res.sendfile('./public/index.html');
});
app.listen(5000);
'use strict';
var gulp = require('gulp');
var browserSync = require('browser-sync');
var nodemon = require('gulp-nodemon');
gulp.task('default', ['browser-sync'], function () {
});
gulp.task('browser-sync', ['nodemon'], function() {
browserSync.init(null, {
proxy: "http://localhost:5000",
files: ["public/**/*.*"],
browser: "google chrome",
port: 7000,
});
});
gulp.task('nodemon', function (cb) {
var started = false;
return nodemon({
script: 'app.js'
}).on('start', function () {
// to avoid nodemon being started multiple times
// thanks @matthisk
if (!started) {
cb();
started = true;
}
});
});
<html>
<head>
<link rel="stylesheet" href="site.css"/>
</head>
<body>
<!-- some dummy non-conforming quick dirty html code to test browser-sync. dont't judge me. -->
<b>lorem</b> ipsum upsum dumsum
</body>
</html>
<!-- put this under /public/* -->
{
"name": "gulp-expressjs-nodemon-browser-sync",
"version": "0.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": {
"name": "Hafiz Ismail",
"email": "hafiz@wehavefaces.net",
"url": "http://wehavefaces.net"
},
"license": "ISC",
"dependencies": {
"express": "^4.8.2"
},
"devDependencies": {
"browser-sync": "^1.3.3",
"gulp": "^3.8.7",
"gulp-nodemon": "^1.0.4"
}
}
/* some css */
/* put this under /public/* */
body {
font-family: 'Helvetica';
color: red;
font-size:24px;
}
@webuniverseio
Copy link
Author

According to API, var browserSync = require('browser-sync'); is changed to var browserSync = require('browser-sync').create(); in 2.0.


For those who cannot run google chrome in windows:

Internally, browser-sync uses opn library. So, the name of browser is different from OS to OS. As for windows, it's not google chrome, it's just chrome. Other browsers:

Internet Explorer: iexplore
Firefox: firefox
Microsoft Edge: path.join(__dirname, ".bin", "edge.exe")
As Microsoft Edge doesn't have the way to call it by cmd start "some command for edge", so we need special command line program. I've downloaded it to projectRoot/.bin and changed its name to "edge.exe". So, the name of edge is really odd. However, when every setup is done, everything works nicely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment