Last active
August 29, 2015 14:14
-
-
Save bogdanbiv/9904709ed9370063464b to your computer and use it in GitHub Desktop.
Reloading Connect/Express server code using grunt-contrib-watch and reload option
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*~ | |
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var bunyan = require('bunyan'); | |
var uuid = require('uuid'); | |
var util = require("util"); | |
var app = express(); | |
function reqSerializer(req) { | |
return { | |
method: req.method, | |
url: req.url, | |
headers: req.headers | |
} | |
} | |
var log = bunyan.createLogger({ | |
name: 'grunt-express-example', | |
streams: [ | |
{ | |
level: 'error', | |
stream: process.stdout // log ERRORand above to stdout | |
}, | |
{ | |
level: 'info', | |
path: __dirname + '/access.log' // log INFO and above to a file | |
} | |
], | |
src: true, | |
serializers: bunyan.stdSerializers // {req: reqSerializer } | |
}); | |
log.info("hi"); | |
app.use(function(req, res, next) { | |
req.log = log.child({reqId: uuid()} ); | |
next(); | |
}); | |
app.get('/', function(req, res, next) { | |
log.info({req: req}, "GET"); | |
next(); | |
}); | |
app.get('/pub/test', function(req, res, next) { | |
log.info({req: req}, "GET fffbivvv43"); | |
next(); | |
}); | |
app.get('/pub', function(req, res, next) { | |
log.info({req: req}, "GET bivvv48"); | |
next(); | |
}); | |
app.use(express.static(__dirname + '/')); | |
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var fs = require('fs'); | |
var path = require('path'); | |
module.exports = function(grunt){ | |
var rq = require('load-grunt-tasks')(grunt); | |
// console.log(rq); | |
grunt.initConfig({ | |
pkg: grunt.file.readJSON( 'package.json' ), | |
express: { | |
server : { | |
options : { | |
port : 9001, | |
hostname : '*', | |
bases : ['./api.js'], | |
livereload : true, | |
showStack: true, | |
server : path.resolve('./api.js'), | |
}, | |
} | |
}, | |
watch : { | |
configFiles: { | |
files: [ 'Gruntfile.js', 'config/*.js' ], | |
tasks: [], // adding connect:server here does run the task with the latest code | |
// (checked with changing onCreateServer console log message) | |
// does not let connect properly listen to HTTP requests | |
options: { | |
livereload : true, | |
reload: true, // DOES not work // | |
} | |
}, | |
appFiles: { | |
files: ['**/*.js', '**/*.json'], | |
options : { | |
livereload : true, | |
reload: true, // DOES not work | |
} | |
} | |
}, | |
connect: { | |
server: { | |
options: { | |
port : 9001, | |
hostname : '*', | |
bases : ['./api.js'], | |
livereload : true, | |
showStack: true, | |
middleware: [ | |
function myMiddleware(req, res, next) { | |
res.end('Hello, world!'); | |
} | |
], | |
onCreateServer: function(server, connect, options) { | |
var fsssss = 4; | |
console.log('onCreateServer:g ' + new Date()); | |
} | |
}, | |
} | |
}, | |
});; | |
grunt.registerTask('server', ['connect:server', 'watch']); // 'watch', 'express-keepalive' | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Exampless |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "grunt-express-example", | |
"version": "0.0.0", | |
"description": "Grunt Express Example", | |
"main": "Gruntfile.js", | |
"scripts": { | |
"test": "grunt test" | |
}, | |
"devDependencies": { | |
"grunt-contrib-watch": "~0.6.1", | |
"grunt-express": "~1.4.1", | |
"load-grunt-tasks": "~3.0.0", | |
"grunt-contrib-connect": "~0.9.0" | |
}, | |
"dependencies": { | |
"express": "~4.1.1", | |
"morgan": "~1.4.1", | |
"bunyan": "~1.2.3", | |
"uuid": "~2.0.1" | |
}, | |
"keywords": [ | |
"grunt", | |
"express" | |
], | |
"author": "Bogdan Bivolaru <https://github.com/bogdanbiv>", | |
"license": "BSD-2-Clause" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment