Gathering detailed insights and metrics for grunt-contrib-connect
Gathering detailed insights and metrics for grunt-contrib-connect
Gathering detailed insights and metrics for grunt-contrib-connect
Gathering detailed insights and metrics for grunt-contrib-connect
npm install grunt-contrib-connect
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
714 Stars
289 Commits
145 Forks
19 Watching
1 Branches
46 Contributors
Updated on 03 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-10.1%
20,275
Compared to previous day
Last week
-1.9%
116,109
Compared to previous week
Last month
-0.7%
537,670
Compared to previous month
Last year
23.5%
6,828,749
Compared to previous year
Start a connect web server
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:
1npm install grunt-contrib-connect --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
1grunt.loadNpmTasks('grunt-contrib-connect');
Run this task with the grunt connect
command.
Note that this server only runs as long as grunt is running. Once grunt's tasks have completed, the web server stops. This behavior can be changed with the keepalive option, and can be enabled ad-hoc by running the task like grunt connect:keepalive
.
This task was designed to be used in conjunction with another task that is run immediately afterwards, like the grunt-contrib-qunit plugin qunit
task.
Type: Integer
Default: 8000
The port on which the webserver will respond. The task will fail if the specified port is already in use (unless useAvailablePort is set). You can use the special values 0
or '?'
to use a system-assigned port.
Type: String
Default: 'http'
May be 'http'
, 'http2'
or 'https'
.
Type: String
Default: '0.0.0.0'
The hostname on which the webserver can be accessed.
Setting it to '*'
, like '0.0.0.0
', will make the server accessible from any local IPv4 address like '127.0.0.1'
and the IP assigned to an ethernet or wireless interface (like '192.168.0.x'
or '10.0.0.x'
). More info
If open
is set to true
, the hostname
setting will be used to generate the URL that is opened by the browser, defaulting to localhost
if a wildcard hostname was specified.
Type: String
or Array
or Object
Default: '.'
Type | Result | Example |
---|---|---|
String | The base (or root) directory from which files will be served. Defaults to the project Gruntfile's directory. | 'public' |
Array | Array of String (or Object ) bases to serve multiple directories. The last base given will be the [directory][] to become browse-able. | ['public','www-root'] |
Object | Map containing path and options keys. options are passed on to the serve-static module. | { path: 'public', options: { maxAge: 1000*60*5 } } |
Type: String
Default: null
Set to the directory you wish to be browse-able. Used to override the base
option browse-able directory.
See https://www.npmjs.com/package/serve-index for details.
Type: Boolean
Default: false
Keep the server alive indefinitely. Note that if this option is enabled, any tasks specified after this task will never run. By default, once grunt's tasks have completed, the web server stops. This option changes that behavior.
This option can also be enabled ad-hoc by running the task like grunt connect:targetname:keepalive
Type: Boolean
Default: false
Set the debug
option to true to enable logging instead of using the --debug
flag.
Type: Boolean
, Number
, or Object
Default: false
Set to anything but false
to inject a live reload script tag into your page using connect-livereload.
If you set to true
, defaults are used. If you set to a number, that number is used as a port number, together with the hostname you configured. If you set this to an object, that object is passed to connect-livereload
unchanged as its configuration.
This does not by itself perform live reloading. It is intended to be used in tandem with grunt-contrib-watch or another task that will trigger a live reload server upon files changing.
Type: Boolean
or String
or Object
Default: false
Open the served page in your default browser.
This can be one of the following:
Specifying true
opens the default server URL (generated from the protocol
, hostname
and port
settings)
Specifying a URL opens that URL
Specify an object with the following keys to configure opn directly:
1{ 2 target: 'http://localhost:8000', // target url to open 3 appName: 'open', // name of the app that opens, ie: open, start, xdg-open 4 callback: function() {} // called when the app has opened 5}
Note that in v0.9.0 open was replaced with opn but the configuration remained the same for backwards compatibility. target
, appName
and callback
are the only supported keys in the config object.
Type: Boolean
Default: false
If true
the task will look for the next available port after the set port
option.
Type: Function
or Array
Default: null
A function to be called after the server object is created, to allow integrating libraries that need access to connect's server object. A Socket.IO example:
1grunt.initConfig({ 2 connect: { 3 server: { 4 options: { 5 port: 8000, 6 hostname: '*', 7 onCreateServer: function(server, connect, options) { 8 var io = require('socket.io').listen(server); 9 io.sockets.on('connection', function(socket) { 10 // do something with socket 11 }); 12 } 13 } 14 } 15 } 16});
Type: Function
or Array
Default: Array
of connect middlewares that use options.base
for static files and directory browsing
As an Array
:
1grunt.initConfig({ 2 connect: { 3 server: { 4 options: { 5 middleware: [ 6 function myMiddleware(req, res, next) { 7 res.end('Hello, world!'); 8 } 9 ], 10 }, 11 }, 12 }, 13});
As a function
:
1grunt.initConfig({ 2 connect: { 3 server: { 4 options: { 5 middleware: function(connect, options, middlewares) { 6 // inject a custom middleware into the array of default middlewares 7 middlewares.unshift(function(req, res, next) { 8 if (req.url !== '/hello/world') return next(); 9 10 res.end('Hello, world from port #' + options.port + '!'); 11 }); 12 13 return middlewares; 14 }, 15 }, 16 }, 17 }, 18});
Lets you add in your own Connect middlewares. This option expects a function that returns an array of middlewares. See the project Gruntfile and project unit tests for a usage example.
In this example, grunt connect
(or more verbosely, grunt connect:server
) will start a static web server at http://localhost:9001/
, with its base path set to the www-root
directory relative to the gruntfile, and any tasks run afterwards will be able to access it.
1// Project configuration. 2grunt.initConfig({ 3 connect: { 4 server: { 5 options: { 6 port: 9001, 7 base: 'www-root' 8 } 9 } 10 } 11});
If you want your web server to use the default options, just omit the options
object. You still need to specify a target (uses_defaults
in this example), but the target's configuration object can otherwise be empty or nonexistent. In this example, grunt connect
(or more verbosely, grunt connect:uses_defaults
) will start a static web server using the default options.
1// Project configuration. 2grunt.initConfig({ 3 connect: { 4 uses_defaults: {} 5 } 6});
You can specify multiple servers to be run alone or simultaneously by creating a target for each server. In this example, running either grunt connect:site1
or grunt connect:site2
will start the appropriate web server, but running grunt connect
will run both. Note that any server for which the keepalive option is specified will prevent any task or target from running after it.
1// Project configuration.
2grunt.initConfig({
3 connect: {
4 site1: {
5 options: {
6 port: 9000,
7 base: 'www-roots/site1'
8 }
9 },
10 site2: {
11 options: {
12 port: 9001,
13 base: 'www-roots/site2'
14 }
15 }
16 }
17});
Options for the serve-static module. See serve-static:
1grunt.initConfig({ 2 connect: { 3 server: { 4 options: { 5 port: 8000, 6 base: { 7 path: 'www-root', 8 options: { 9 index: 'somedoc.html', 10 maxAge: 300000 11 } 12 } 13 } 14 } 15 } 16});
Like the Basic Use example, this example will start a static web server at http://localhost:9001/
, with its base path set to the www-root
directory relative to the gruntfile. Unlike the other example, this is done by creating a brand new task. in fact, this plugin isn't even installed!
1// Project configuration. 2grunt.initConfig({ /* Nothing needed here! */ }); 3 4// After running "npm install connect serve-static --save-dev" to add connect as a dev 5// dependency of your project, you can require it in your gruntfile with: 6var connect = require('connect'); 7var serveStatic = require('serve-static'); 8connect(serveStatic('www-root')).listen(9001); 9 10// Now you can define a "connect" task that starts a webserver, using the 11// connect lib, with whatever options and configuration you need: 12grunt.registerTask('connect', 'Start a custom static web server.', function() { 13 grunt.log.writeln('Starting static web server in "www-root" on port 9001.'); 14 connect(serveStatic('www-root')).listen(9001); 15});
A default certificate authority, certificate and key file are provided and pre-
configured for use when protocol
has been set to https
.
NOTE: No passphrase set for the certificate. If you are getting warnings in Google Chrome, add 'server.crt' (from 'node_modules/tasks/certs') to your keychain. In OS X, after you add 'server.crt', right click on the certificate, select 'Get Info' - 'Trust' - 'Always Trust', close window, restart Chrome.
For HTTPS / HTTP2 livereload with grunt-contrib-watch see the last example here.
If the default certificate setup is unsuitable for your environment, OpenSSL can be used to create a set of self-signed certificates with a local ca root.
1### Create ca.key, use a password phrase when asked 2### When asked 'Common Name (e.g. server FQDN or YOUR name) []:' use your hostname, i.e 'mysite.dev' 3openssl genrsa -des3 -out ca.key 1024 4openssl req -new -key ca.key -out ca.csr 5openssl x509 -req -days 365 -in ca.csr -out ca.crt -signkey ca.key 6 7### Create server certificate 8openssl genrsa -des3 -out server.key 1024 9openssl req -new -key server.key -out server.csr 10 11### Remove password from the certificate 12cp server.key server.key.org 13openssl rsa -in server.key.org -out server.key 14 15### Generate self-siged certificate 16openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
For more details on the various options that can be set when configuring SSL, please see the Node documentation for TLS.
Grunt configuration would become
1// Project configuration.
2grunt.initConfig({
3 connect: {
4 server: {
5 options: {
6 protocol: 'https', // or 'http2'
7 port: 8443,
8 key: grunt.file.read('server.key').toString(),
9 cert: grunt.file.read('server.crt').toString(),
10 ca: grunt.file.read('ca.crt').toString()
11 },
12 },
13 },
14});
The connect plugin will emit a grunt event, connect.{taskName}.listening
, once the server has started. You can listen for this event to run things against a keepalive server, for example:
1grunt.registerTask('jasmine-server', 'start web server for jasmine tests in browser', function() { 2 grunt.task.run('jasmine:tests:build'); 3 4 grunt.event.once('connect.tests.listening', function(host, port) { 5 var specRunnerUrl = 'http://' + host + ':' + port + '/_SpecRunner.html'; 6 grunt.log.writeln('Jasmine specs available at: ' + specRunnerUrl); 7 require('open')(specRunnerUrl); 8 }); 9 10 grunt.task.run('connect:tests:keepalive'); 11});
util.isArray
.node-http2
. Add secureProtocol
in httpsOptions
. Fix open.appName
. Allow passing serve-index
options.opn
as it fixes some Linux issues. Add support for connect.static
instance options.hostname
option.0.0.0.0
. Modified options.middleware
to accept an array or a function.options.hostname
if provided. Update connect-livereload to ~0.3.0. Update connect to ~2.12.0. Use well-formed SSL certificates. Support all options of open. Make directory browseable when base is a string.open
option.options.base
as it should be a string or an array as the user has set. Fix setting target hostname
option.debug
option added to display debug logging like the --debug
flag. livereload
option added to inject a livereload snippet into the page.Task submitted by "Cowboy" Ben Alman
This is a generated file.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
3 existing vulnerabilities detected
Details
Reason
5 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 4
Reason
Found 3/10 approved changesets -- score normalized to 3
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More