Grunt HTTP Static Server
grunt-http-server is a Grunt task designed to serve static files from within a Grunt build process. It allows developers to quickly set up a local HTTP or HTTPS server for development and testing purposes. The package, currently at version 2.1.0 (last published in 2015), offers features such as serving files from a specified root directory, configurable ports and hosts, caching, directory listing, automatic index file serving, custom file extensions, and a proxying capability for requests not resolved locally. It also supports HTTPS with custom certificates and allows opening a browser automatically. Its primary differentiation lies in its integration with the Grunt ecosystem, providing a task-based approach to running a simple web server, unlike modern standalone CLI tools or bundler-specific development servers. The project has not seen updates since 2016, indicating it is no longer actively maintained.
Common errors
-
Warning: Task "http-server:dev" not found.
cause The Grunt task was not loaded correctly, or the task name in `registerTask` does not match the configured task.fixEnsure `grunt.loadNpmTasks('grunt-http-server');` is present in your Gruntfile.js and that the task name used in `grunt.registerTask` (e.g., 'http-server:dev') matches your configuration. -
Error: listen EADDRINUSE: address already in use :::8080
cause The specified `port` (e.g., 8080) in the `http-server` configuration is already in use by another application or process on your system.fixChange the `port` option in your `http-server` configuration to an available port number (e.g., 8000, 9000), or identify and terminate the process currently using that port. -
Server started at http://0.0.0.0:8282, but files are not loading or return 404.
cause The `root` path specified in the `http-server` configuration does not correctly point to the directory containing your static files.fixVerify that the `root` option in your `http-server` configuration specifies the correct absolute or relative path to your static asset directory, relative to your Gruntfile.js.
Warnings
- gotcha The `grunt-http-server` package is abandoned and no longer maintained. Its last update was in 2016. Using it may introduce compatibility issues with modern Node.js versions and lacks ongoing security patches. It is not recommended for new projects or production environments.
- gotcha This package was developed for older Node.js versions (>=0.8.0). It may exhibit unexpected behavior, throw errors, or fail to run entirely on modern Node.js runtimes (e.g., Node.js 16+ or 18+).
- gotcha As an unmaintained package, `grunt-http-server` has not received security updates since 2016. It may contain unpatched vulnerabilities, particularly concerning path traversal, directory listing exploits, or other common web server security flaws. Avoid using it in production environments or with untrusted content.
Install
-
npm install grunt-http-server -
yarn add grunt-http-server -
pnpm add grunt-http-server
Imports
- grunt-http-server
import { 'http-server' } from 'grunt-http-server'; // OR const http_server = require('grunt-http-server');grunt.loadNpmTasks('grunt-http-server'); - http-server task configuration
grunt.initConfig({ 'http-server': { 'dev': { root: 'dist', port: 8080 } } });
Quickstart
module.exports = function(grunt) {
grunt.initConfig({
'http-server': {
'dev': {
// the server root directory
root: 'dist', // Assume 'dist' is your build output folder
// the server port
port: 8282,
// the host ip address
host: '0.0.0.0',
cache: 0, // No caching for dev
showDir: true,
autoIndex: true,
ext: 'html',
runInBackground: false,
logFn: function(req, res, error) {},
// proxy: 'http://someurl.com',
https: {
cert: 'path/to/cert.pem', // Replace with your actual cert path
key : 'path/to/key.pem' // Replace with your actual key path
},
openBrowser : true,
customPages: {
'/readme': 'README.md'
}
}
}
});
// Load the plugin that provides the 'http-server' task.
grunt.loadNpmTasks('grunt-http-server');
// Default task.
grunt.registerTask('default', ['http-server:dev']);
};