Git Product home page Git Product logo

server-configs-node's Introduction


⚠️ This module is depecrated (Express 3.x) and is being rewritten.


HTML5 boilerplate (H5BP) server config for node.js.

h5bp for node.js follows the guidelines of the Apache version:

  • secures backup and hidden files.
  • optionally redirects www.yoursite.tld to yoursite.tld or vice versa.
  • offers a simple cache busting mechanism.
  • normalize content types.
  • optionally enables CORS.
  • sets correct cache expires depending of the type of resource.
  • and some others...

It also focuses on offering additional features such as on-the-fly script concatenation using CommonJS or AMD.

Installation

npm install --save h5bp

Quick Start

Create a simple http server

var h5bp = require('h5bp');

var app = h5bp.createServer({ root: __dirname + '/public' });
app.listen(3000);

app is an instance of an express application. You can add additional middlewares or routes if you like.

Use it as a connect / express middleware

var express = require('express'),
    h5bp = require('h5bp');

var app = express();
app.use(h5bp({ root: __dirname + '/public' }));

// in order to serve files, you should add the two following middlewares
app.use(express.compress());
app.use(express.static(__dirname + '/public'));
app.listen(3000);

Concatenate scripts on-the-fly

If you want to split your application source files but only serve one file, you can use the on-the-fly concatenation. If you are familiar with node.js, you can use the CommonJS style. You can also use the AMD style.

app.use(h5bp({
    root: __dirname + '/public',
    scripts: {
        files: ['app.js'],
        processor: 'commonjs'   // can also be "amd"
    }
}));

At the first request hit to /app.js, the server will compile, cache and serve the file. Any subsequent request will serve the cached file without any performance impact.

So, this feature is meant to be used with the cache busting mechanism in order to ensure the client always has the latest resource version. If you restart your server, the cache will be flushed.

Note that the next release will provide a development mode where the server will simply disable its cache and always serve the latest version of the file.

Options

There are several options you can pass to the middleware.

app.use(h5bp(options));

root

Tells the filesystem path to the root directory of static resources. This options is mandatory if you serve static files.

www

Forces www if true, forces non-www if false, does nothing if not defined. By default, this is disabled.

cors

Enables CORS for everything. By default this is disabled.

dotfiles

Enables access to dotfiles. By default this is disabled.

scripts

Tells which scripts to concatenate.

This is an object with the following properties:

files

This is an array of files to concatenate. Their path is relative to the root option. Their URL will be absolute.

For example, if you set files to ['scripts/app.js'] and root to /home/h5bp/app/:

  • The path will be: /home/h5bp/app/scripts/app.js.
  • The served URL will be: yoursite.tld/scripts/app.js.

processor

Tells which processor to use for scripts concatenation.

For now, it can be one of the following values:

  • commonjs: will concatenate files using the CommonJS method (require/exports).
  • amd: will concatenate files using the AMD method (require/define).

Additional options

The h5bp.createServer function takes the same options, plus additional ones.

The callback is optional. It is a custom middleware that you can register directly if you want to.

h5bp.createServer(options, [callback]);

server

Tells which type of server you want to use.

It can be one of the following values:

  • express: uses express, this is the default value.
  • connect: uses connect.

logger

Tells if you want to log server requests or not. This can also be an object containing logger options.

compress

Tells if you want to serve gzipped content or not. By default this is true.

If you are using h5bp as a middleware, we strongly encourage you to use the compress middleware provided by express / connect.

License

MIT License

server-configs-node's People

Contributors

ad7six avatar alrra avatar arthurvr avatar bnjmnt4n avatar breadbaker avatar chadsmith avatar davidcl64 avatar davidmurdoch avatar jesstelford avatar klaemo avatar matylla avatar mikealmond avatar necolas avatar ngryman avatar niftylettuce avatar sacenox avatar xhmikosr avatar zlatanvasovic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

server-configs-node's Issues

Update npm module

Version bump to 0.0.5 is needed for my latest commits. This update fixes issues reported to me by email that users have encountered.

Nick, please get to this asap.

Add `CHANGELOG.md`

Keep the project history within a (recognized) file, named CHANGELOG.md.

v1.0.0 roadmap

Hi guys,

server-configs-node is aging and offers features like style and javascript processing that may be out of scope. I would like to propose a full clean rewrite that only reflects other server configs.

This project is a little different than other server-configs-* projects since it's a server. But the community is already using existing libraries like express to create servers. So I think we should only propose a universal middleware that would work in any context, instead of a server.
We could officially support most used libraries by default (express, builtin, others?).

This rewrite would aim the 1.0.0 release and freeze the api to a single function taking options (like any middleware). A migration guide would be provided for users that want to upgrade and keep existing style and javascript processing.
The current legacy version would be supported in a separated branch.

@h5bp/owners @h5bp/server-configs What do you think? Do you have any feedback on this?

Thanks!

Do we need `cache.js` in the repo?

It's third party code so let's see how we can pull it in using npm.

Or even better, search another solution as the cache.js code dates from 2011.

EventEmitter memory leak detected.

I am using jmeter to test the performance of my code.For that i am firing 50 requests at a time but the code takes only 11 requests and and gives the warning as follow.

I am using h5bp npm library to compress the data send over the network.

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
at ServerResponse.addListener (events.js:160:15)
at headersLayer (C:\Users\Pratik\Documents\brigge\source\node_modules\h5bp\lib\layers\headers.js:133:7)
at fn (C:\Users\Pratik\Documents\brigge\source\node_modules\h5bp\node_modules\async\lib\async.js:582:34)
at Object._onImmediate (C:\Users\Pratik\Documents\brigge\source\node_modules\h5bp\node_modules\async\lib\async.js:49
8:34)
at processImmediate as _immediateCallback

Explode tests in several files

Also refator some parts.
I'm thinking about the standalone section which is now, internally, the same as express/connect.

Umm... how do I use this?

Was expecting to see something like "app.js" in the root directory calling /lib/h5bp.js. But with no example code/app and no documentation I'm puzzled how I can get the value out of what is obviously a lot of thought and expertise backed into h5BP.js.

Contribute to the project

Hi guys,

I proposed a few month ago a big refactoring for the node implementation of the h5bp server configs.
I was wishing it would become a more attractive and better tested project.
Nowadays I can see that tests don't even run and the project does contains major bugs that make it unusable :'(

So I would like to propose myself to become an official contributor, and eventually one of the maintainers for this project.

What do you think about that ?

Requesting change to mime type logic

Currently the logic in headers looks up mime type and sets the Content-Type header whenever the path doesn't end in a trailing /

I'd like to see this improved in two ways.

First - add a check to see if Content-Type is already set and skip this logic if that is the case.

Second, change the regex to check for the existence of an extension at the end of the path as the comments to the code state rather than just whether the path ends with a trailing /.

The reason for my request may be a bit contentious as there is a bit of a religious war going on as to whether a resource path must end in a trailing slash if it does not reference a file resource. However I believe these two changes would make the logic more correct irrespective of how I'd like the behavior to change.

As it stands now, accessing a resource such as http://foo.com/bar returns a content-type of application/octet-stream with no good way to override the behavior.

Cannot find module "express/node_modules/send"

When starting my application that uses express and h5bp as express middleware:

Error: Cannot find module 'express/node_modules/send'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (/home/brenton/Code/Projects/timestamper-fcc/node_modules/h5bp/lib/layers/headers.js:6:45)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)

h5bp.createServer throws error

Hi guys, I'm new in node.js, and I'm not sure what I am doing wrong
i have installed h5bp npm package, included in my app.js and tried to create server with express option as it is in the example (I read in issues that npm is updated). but the console keep saying that there is no such method. I'm a big fan of html5 bolerplate, I used it in several project with apache and iis but never with node.js. Any help will be appreciated. If you say that it's not finished yet I'll understand, but it seems it is. all the rules from htaccess are kinda included in h5bp.js.

server-configs-node future

Hi @h5bp/server-configs folks!

This module is aging and mostly deprecated. Node.js ecosystem has since evolved a lot and there is a plethora of server frameworks that address (in a better way) most of the best practices we try to advocate here.

Since all those frameworks and their ecosystem already implement the code and maintain it, I think it makes no sense to do it here. Our goal is to tell people how to configure servers in the best way possible, not to implement servers.

Given the context, I propose that this repo evolve from plain code to How to configure X framework properly? We would need to list the most used frameworks out there and document how to configure them properly.

This could be done either by:

  1. Creating one markdown document per framework and explain how to configure it for each key best practice.
  2. Creating a heavily commented boilerplate per framework that one could use directly or cherry pick from.

What do you think about this? Do you have any ideas/remarks?

Thanks!

Update npm package and installation instructions

  • Update npm package to point to correct git repo URL (following the recent rename).

  • Fix / remove the image banner in the README as the link 404's.

  • Add more maintainers to the package.

  • Include a proper CLI command, e.g.:

    npm install --save <name>

Related: #11 - rename the package.

Defer setHeader/*

Because other middlewares can generate dynamic content w/ an appropriate content-type but with a custom url, we should defer setting other headers by subscribing to the .on('header', ...) event.

We should continue relying on the given content-type to apply cache and other stuff.

By defering, we also ensure that we have the last word and that nobody will override what we set.

By providing dynamic content, the user takes the responsability to match the correct content-type correctly.

If nobody sets the content-type it should be defaulted to application/octet-stream (the current behavior).

Update some `express` calls

express has updated its API or provides some methods we should rely on:

  • Response#type
  • Response#set
  • Response#get

Static html is served as download content

Hi, I have included today h5bp as middleware in my express app, it's added exactly as in your example, before compress and static content. I think something is wrong with the header. Here is what I get from network tab in Chrome. It's Content-Type:application/octet-stream. Can you please help me with configuration, I'm using this example as testing platform

https://github.com/madhums/nodejs-express-mongoose-demo

Request URL:http://localhost:3000/
Request Method:GET
Status Code:206 Partial Content

Request Headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en,bg;q=0.8,en-US;q=0.6
Connection:keep-alive
Host:localhost:3000
If-Range:Sun, 24 Feb 2013 16:42:41 GMT
Range:bytes=3811-3811
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22

Response Headers
Accept-Ranges:bytes
cache-control:public,max-age=2419200,no-transform
connection:keep-alive
Content-Length:1
Content-Range:bytes 3811-3811/6738
Content-Type:application/octet-stream
Date:Sun, 24 Feb 2013 16:58:35 GMT
Last-Modified:Sun, 24 Feb 2013 16:42:41 GMT
Vary:Accept-Encoding

Scripts and stylesheets minification

So for current processors, we should enable minification:

  • automatically when process.NODE_ENV is set to production.
  • with an additional option, which take precedence over automatic (we could force no minification in production).

Affected processors are:

  • amd
  • commonjs
  • sass
  • less
  • stylus

Once this issue completed, I think we are good to release 0.0.7!

concatenation & minification

First todo in my list is this feature.

With node we could do this on-the-fly without any build step involved.

Concatenation

Convention over configuration

As the Apache version, we could say that every js file in a given directory should be automatically concatened to a given filename. We could default libs.js and app.js. This would be an automatic process.

I think we can do much better with node. The only thing I find usefull is that this is very simple for beginners. In fact this could even be a default configuration for any other approach.

Configuration over convention

We could let the user configure exactly which scripts to concatenate and in what order. As the previous approach, we could propose two defaults targets libs.js and app.js.

I think this is better that the previous approach because we don't loose any best practice value by defaulting the configuration. We let the user refine the order of the scripts and define additional targets.
But I also think that maintaining a configuration like this is painful, error prone ... and so 2010...

Dependency management

CommonJS or AMD could (and should) be our friends for client-side scripts. There is a lot of good libraries offering a client-side implementation and I think this should be a 2013 best practice for font-end developers, with Harmony incoming.

With node we could easily get this done server-side (i.e. Browserify) on-the-fly.

The user could configure target scripts like this:

app.use(h5bp({
  root: __dirname + '/public',
  scripts: ['app.js', 'libs.js']
}));

Internally h5bp will start watching for file changes (Browserify does this well) and compile if needed. The static middleware will simply serve compiled files.

Minification

For minification, we could use the same approach of concatenation (i.e. UglifyJS 2) by proxying requests to javascripts, cache local minified version and internal redirect to it.

The user could enable minification like this:

app.use(h5bp({
  root: __dirname + '/public',
  scripts: ['app.js', 'libs.js'],
  minify: true
}));

In the future we could also default minification when NODE_ENV=production is defined for example.

@paulirish, do you think we could go with something like this ?

EDIT: we could also always minify and use source maps when NODE_ENV!=production

Rename package

The name "h5bp" is vague and it feels confusing to have a server config package named after a software organization. I'd suggest that an alternative name (that's clearer) is used at some point in the future.

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.