Code Monkey home page Code Monkey logo

grunt-express-server's Introduction

grunt-express-server Build Status

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 before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

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:

grunt.loadNpmTasks('grunt-express-server');

The express task

Setup

In your project's Gruntfile, you can create one or multiple servers:

grunt.initConfig({
  express: {
    options: {
      // Override defaults here
    },
    dev: {
      options: {
        script: 'path/to/dev/server.js'
      }
    },
    prod: {
      options: {
        script: 'path/to/prod/server.js',
        node_env: 'production'
      }
    }
    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

  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,

      // Override node env's NODE_ENV
      node_env: undefined,

      // 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, upon which control is passed back to grunt.

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 express:dev.

Stopping the server

Similarly, if you start the dev server with express:dev, you can stop the server with express:dev:stop.

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.

Release History

v0.4.1

  • Add node_env option that sets NODE_ENV when running the server & restores original env upon closing, thanks to @jgable!

v0.4.0

  • Add delay option that, when set, passes control back to grunt after timeout
  • Add output regular expression option that, when set, waits for matching message before passing control back to grunt

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

grunt-express-server's People

Contributors

ericclemmons avatar jgable avatar mpderbec avatar outring avatar paulgrock avatar yosuke-furukawa avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.