.gitignore000066400000000000000000000000371516067361400130570ustar00rootroot00000000000000node_modules npm-debug.log tmp .jshintrc000066400000000000000000000003161516067361400127140ustar00rootroot00000000000000{ "curly": true, "eqeqeq": true, "immed": true, "latedef": true, "newcap": true, "noarg": true, "sub": true, "undef": true, "boss": true, "eqnull": true, "node": true, "es5": true } .travis.yml000066400000000000000000000000431516067361400131750ustar00rootroot00000000000000language: node_js node_js: - 0.8 Gruntfile.js000066400000000000000000000051541516067361400133710ustar00rootroot00000000000000/* * grunt-express-server * https://github.com/ericclemmons/grunt-express-server * * Copyright (c) 2013 Eric Clemmons * Licensed under the MIT license. */ 'use strict'; module.exports = function(grunt) { // Project configuration. grunt.initConfig({ jshint: { all: [ 'Gruntfile.js', 'tasks/*.js', 'tests/*.js', ], options: { jshintrc: '.jshintrc', }, }, // Before generating any new files, remove any previously-created files. clean: { tests: ['tmp'], }, // Unit tests. nodeunit: { defaults: { src: 'test/defaults_test.js' }, custom_args: { src: 'test/custom_args_test.js' }, custom_port: { src: 'test/custom_port_test.js' }, custom_delay: { src: 'test/custom_delay_test.js' }, custom_output: { src: 'test/custom_output_test.js' }, stoppable: { src: 'test/stoppable_test.js' } }, express: { options: { script: './test/server.js', port: 3000 }, defaults: {}, custom_args: { options: { args: [ 1, 2], output: "Express server listening on port .+" } }, custom_port: { options: { port: 8080, output: "Express server listening on port .+" } }, custom_delay: { options: { delay: 1000, output: "This RegEx does not match anything lol" } }, custom_output: { options: { output: "timeout" } }, custom_delay_output: { options: { delay: 1000, output: "Express server listening on port .+" } }, stoppable: {} } }); // Actually load this plugin's task(s). grunt.loadTasks('tasks'); // These plugins provide necessary tasks. grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-clean'); grunt.loadNpmTasks('grunt-contrib-nodeunit'); // Whenever the "test" task is run, first clean the "tmp" dir, then run this // plugin's task(s), then test the result. grunt.registerTask('test', [ 'clean', 'express:defaults', 'nodeunit:defaults', 'express:custom_args', 'nodeunit:custom_args', 'express:custom_port', 'nodeunit:custom_port', 'express:custom_delay', 'nodeunit:custom_delay', 'express:custom_output', 'nodeunit:custom_output', 'express:stoppable', 'express:stoppable:stop', 'nodeunit:stoppable' ]); // By default, lint and run all tests. grunt.registerTask('default', ['jshint', 'test']); }; LICENSE-MIT000066400000000000000000000020411516067361400125200ustar00rootroot00000000000000Copyright (c) 2013 Eric Clemmons Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. README.md000066400000000000000000000104631516067361400123520ustar00rootroot00000000000000# grunt-express-server [![Build Status](https://travis-ci.org/ericclemmons/grunt-express-server.png?branch=master)](https://travis-ci.org/ericclemmons/grunt-express-server) > Simple grunt task for running an Express server that works great with LiveReload + Watch/Regarde ## Getting Started This plugin requires Grunt `~0.4.0` If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command: ```shell npm install grunt-express-server --save-dev ``` One the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript: ```js grunt.loadNpmTasks('grunt-express-server'); ``` ## The `express` task ### Setup In your project's Gruntfile, you can create one or multiple servers: ```js grunt.initConfig({ express: { options: { // Override defaults here }, dev: { options: { script: 'path/to/dev/server.js' } }, prod: { options: { script: 'path/to/prod/server.js' } } test: { options: { script: 'path/to/test/server.js' } } } }); ``` You can override the default `options` either in the root of the `express` config or within each individual server task. ### Default `options` ```js express: { options: { // Will turn into: `node path/to/server.js ARG1 ARG2 ... ARGN` args: [ ], // Setting to `false` will effectively just run `node path/to/server.js` background: true, // Called if spawning the server fails error: function(err, result, code) {}, // Called when the spawned server throws errors fallback: function() {}, // Override node env's PORT port: 3000, // Consider the server to be "running" after an explicit delay (in milliseconds) // (e.g. when server has no initial output) delay: 0, // Regular expression that matches server output to indicate it is "running" output: ".+" } } ``` ### Usage By default, unless `delay` or `output` has been customized, **the server is considered "running" once any output is logged to the console**. Typically, this is normally: > Express server listening on port 3000 If you log output *before* the server is running, either set `delay` or `output` to indicate when the server has officially started. #### Starting the server If you have a server defined named `dev`, you can start the server by running `expess:dev`. #### Stopping the server Similarly, if you start the `dev` server with `express:dev`, you can stop the server with `express:dev:stop`. #### With [grunt-contrib-livereload](https://github.com/gruntjs/grunt-contrib-livereload) ```js grunt.initConfig({ watch: { express: { files: [ 'path/to/files/to/watch/**.js' ], tasks: [ 'express:dev', 'livereload' ] } } }); grunt.registerTask('server', [ 'express:dev', 'livereload', 'watch' ]) ``` This will let you override `grunt server` with a LiveReload-able Express Server. Finally, you can make changes to your API and watch the JSON change in your browser! #### With [grunt-contrib-watch](https://github.com/gruntjs/grunt-contrib-watch) ```js grunt.initConfig({ watch: { express: { files: [ '**/*.js' ], tasks: [ 'express:dev' ], options: { nospawn: true //Without this option specified express won't be reloaded } } } }); grunt.registerTask('server', [ 'express:dev', 'watch' ]) ``` ## Contributing In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/). ## Release History ### v0.3.1 - Try to force notification that `express` task has finished as much as possible ### v0.3.0 - `express` is now a multitask with customizable options, better error handling and `:stop` task ### v0.2.0 - Change `express-server` task to `express` - Config is set via `express: '...'` instead of `server: { script: '...' } ` ### v0.1.0 - Initial import from [Genesis Skeleton](https://github.com/ericclemmons/genesis-skeleton) & release package.json000066400000000000000000000021561516067361400133610ustar00rootroot00000000000000{ "name": "grunt-express-server", "description": "Grunt task for running an Express Server that works great with LiveReload + Watch/Regarde", "version": "0.3.1", "homepage": "https://github.com/ericclemmons/grunt-express-server", "author": { "name": "Eric Clemmons", "email": "eric@smarterspam.com" }, "repository": { "type": "git", "url": "git://github.com/ericclemmons/grunt-express-server.git" }, "bugs": { "url": "https://github.com/ericclemmons/grunt-express-server/issues" }, "licenses": [ { "type": "MIT", "url": "https://github.com/ericclemmons/grunt-express-server/blob/master/LICENSE-MIT" } ], "main": "Gruntfile.js", "engines": { "node": ">= 0.8.0" }, "scripts": { "test": "./node_modules/.bin/grunt" }, "devDependencies": { "express": "~3.2.4", "grunt": "~0.4.0", "grunt-cli": "~0.1.6", "grunt-contrib-jshint": "~0.1.1", "grunt-contrib-clean": "~0.4.0", "grunt-contrib-nodeunit": "~0.1.2" }, "peerDependencies": { "grunt": "~0.4.0" }, "keywords": [ "gruntplugin", "express", "server" ] } tasks/000077500000000000000000000000001516067361400122145ustar00rootroot00000000000000tasks/express.js000066400000000000000000000017321516067361400142460ustar00rootroot00000000000000/* * grunt-express-server * https://github.com/ericclemmons/grunt-express-server * * Copyright (c) 2013 Eric Clemmons * Licensed under the MIT license. */ 'use strict'; var path = require('path'); module.exports = function(grunt) { var server = require('./lib/server')(grunt); grunt.registerMultiTask('express', 'Start an express web server', function() { var options = this.options({ args: [ ], background: true, error: function(err, result, code) { /* Callback has to exist */ }, fallback: function() { /* Prevent EADDRINUSE from breaking Grunt */ }, port: 3000, delay: 0, output: ".+" }); options.script = path.resolve(options.script); options.args.unshift(options.script); if (!grunt.file.exists(options.script)) { grunt.log.error('Could not find server script: ' + options.script); return false; } server.start(options); }); }; tasks/lib/000077500000000000000000000000001516067361400127625ustar00rootroot00000000000000tasks/lib/server.js000066400000000000000000000036411516067361400146320ustar00rootroot00000000000000/* * grunt-express-server * https://github.com/ericclemmons/grunt-express-server * * Copyright (c) 2013 Eric Clemmons * Licensed under the MIT license. */ 'use strict'; module.exports = function(grunt) { var done = null; var server = null; // Store server between live reloads to close/restart express var finished = function() { if (done) { done(); done = null; } }; return { start: function(options) { if (server) { this.stop(); if (grunt.task.current.flags.stop) { finished(); return; } }; grunt.log.writeln('Starting '.cyan + (options.background ? 'background' : 'foreground') + ' Express server'); done = grunt.task.current.async(); // Set PORT for new processes process.env.PORT = options.port; if (options.background) { server = grunt.util.spawn({ cmd: process.argv[0], args: options.args, env: process.env, fallback: options.fallback }, options.error); if (options.delay) { setTimeout(finished, options.delay); } if (options.output) { server.stdout.on('data', function(data){ var message = "" + data; var regex = new RegExp(options.output, "gi"); if (message.match(regex)) finished(); }); } server.stdout.pipe(process.stdout); server.stderr.pipe(process.stderr); } else { // Server is ran in current process server = require(options.script); finished(); } process.on('exit', finished); process.on('exit', this.stop); }, stop: function() { if (server) { grunt.log.writeln('Stopping'.red + ' Express server'); server.kill('SIGTERM'); process.removeAllListeners(); server = null; }; finished(); } }; }; test/000077500000000000000000000000001516067361400120465ustar00rootroot00000000000000test/app.js000066400000000000000000000006171516067361400131700ustar00rootroot00000000000000var express = require('express'); var app = module.exports = express(); var args = process.argv; app.set('port', process.env.PORT || 3000); app.use(express.static(__dirname + '/../test/fixtures')); // Setup simple echo for each additional argument passed for testing args.slice(2).forEach(function(arg) { app.get('/' + arg, function(req, res) { res.send('Howdy ' + arg + '!'); }); }); test/custom_args_test.js000066400000000000000000000015141516067361400157720ustar00rootroot00000000000000/* * grunt-express-server * https://github.com/ericclemmons/grunt-express-server * * Copyright (c) 2013 Eric Clemmons * Licensed under the MIT license. */ 'use strict'; var get = require('./lib/get'); module.exports.custom_args = { 1: function(test) { test.expect(2); get('http://localhost:3000/1', function(res, body) { test.equal(res.statusCode, 200, 'should return 200'); test.equal(body, 'Howdy 1!', 'should return dynamic page'); test.done(); }, function(err) { test.done(); }); }, 2: function(test) { test.expect(2); get('http://localhost:3000/2', function(res, body) { test.equal(res.statusCode, 200, 'should return 200'); test.equal(body, 'Howdy 2!', 'should return dynamic page'); test.done(); }, function(err) { test.done(); }); } }; test/custom_delay_test.js000066400000000000000000000010751516067361400161360ustar00rootroot00000000000000/* * grunt-express-server * https://github.com/ericclemmons/grunt-express-server * * Copyright (c) 2013 Eric Clemmons * Licensed under the MIT license. */ 'use strict'; var get = require('./lib/get'); module.exports.custom_delay = { test_runs_after_timeout: function(test) { test.expect(2); get('http://localhost:3000/hello.txt', function(res, body) { test.equal(res.statusCode, 200, 'should return 200'); test.equal(body, 'Howdy!\n', 'should return static page'); test.done(); }, function(err) { test.done(); }); } }; test/custom_output_test.js000066400000000000000000000011071516067361400163740ustar00rootroot00000000000000/* * grunt-express-server * https://github.com/ericclemmons/grunt-express-server * * Copyright (c) 2013 Eric Clemmons * Licensed under the MIT license. */ 'use strict'; var get = require('./lib/get'); module.exports.custom_output = { test_runs_after_listening_output: function(test) { test.expect(2); get('http://localhost:3000/hello.txt', function(res, body) { test.equal(res.statusCode, 200, 'should return 200'); test.equal(body, 'Howdy!\n', 'should return static page'); test.done(); }, function(err) { test.done(); }); } }; test/custom_port_test.js000066400000000000000000000010741516067361400160230ustar00rootroot00000000000000/* * grunt-express-server * https://github.com/ericclemmons/grunt-express-server * * Copyright (c) 2013 Eric Clemmons * Licensed under the MIT license. */ 'use strict'; var get = require('./lib/get'); module.exports.custom_port = { test_accessible_on_8080: function(test) { test.expect(2); get('http://localhost:8080/hello.txt', function(res, body) { test.equal(res.statusCode, 200, 'should return 200'); test.equal(body, 'Howdy!\n', 'should return static page'); test.done(); }, function(err) { test.done(); }); } }; test/defaults_test.js000066400000000000000000000011471516067361400152550ustar00rootroot00000000000000/* * grunt-express-server * https://github.com/ericclemmons/grunt-express-server * * Copyright (c) 2013 Eric Clemmons * Licensed under the MIT license. */ 'use strict'; var get = require('./lib/get'); module.exports.defaults = { test_server_not_running: function(test) { test.expect(1); try { get('http://localhost:3000/hello.txt', function(res, body) { throw new Error('Server should not be running yet'); test.done(); }, function(err) { test.equals('ECONNREFUSED', err.code); test.done(); }); } catch (e) { test.done(); } } }; test/fixtures/000077500000000000000000000000001516067361400137175ustar00rootroot00000000000000test/fixtures/hello.txt000066400000000000000000000000071516067361400155600ustar00rootroot00000000000000Howdy! test/lib/000077500000000000000000000000001516067361400126145ustar00rootroot00000000000000test/lib/get.js000066400000000000000000000010031516067361400137230ustar00rootroot00000000000000/* * grunt-express-server * https://github.com/ericclemmons/grunt-express-server * * Copyright (c) 2013 Eric Clemmons * Licensed under the MIT license. */ 'use strict'; var http = require('http'); module.exports = function get(url, callback, error) { var req = http.get(url, function(res) { var body = ''; res.on('data', function(chunk) { body += chunk; }).on('end', function() { callback(res, body); }); }); if (error) { req.on('error', error) }; req.end(); } test/server.js000066400000000000000000000006721516067361400137170ustar00rootroot00000000000000/** * Test Server */ var app = require('./app'); var start = Date.now(); var log = function(message) { console.log("[" + (Date.now() - start) + "] " + message); }; log("Begin server.js"); setTimeout(function() { module.exports = app.listen(app.get('port'), function() { log("Express server listening on port " + app.get('port')); }); }, 50); setTimeout(function() { log("250ms timeout"); }, 250); log("End server.js"); test/stoppable_test.js000066400000000000000000000007651516067361400154440ustar00rootroot00000000000000/* * grunt-express-server * https://github.com/ericclemmons/grunt-express-server * * Copyright (c) 2013 Eric Clemmons * Licensed under the MIT license. */ 'use strict'; var get = require('./lib/get'); module.exports.stoppable = { hello: function(test) { test.expect(1); get('http://localhost:3000/hello.txt', function(res, body) { test.done(); }, function(err) { test.equal('ECONNREFUSED', err.code, 'should return ECONNREFUSED'); test.done(); }); } };