Giter VIP home page Giter VIP logo

lactate's Introduction

Lactate

Very simple static file handler, with a few electives.

npm install lactate

npm test lactate

Benchmark

Preliminary benchmarks show that Lactate has a significant advantage over most worthy competitors on the node modules wiki

See /benchmarks for details

Example

Just pass three arguments to the serve function path [optional], request, response. Lactate will stream your file to the client in the most efficient way, by piping: readFile > gZip > response.

var express = require('express')
var app = express.createServer()

var lactate = require('lactate').Lactate()

app.get('/', function(req, res) {
  return lactate.serve('pages/land.html', req, res)
})

lactate.set('root', 'files')

app.get('/files/*', function(req, res) {
  return lactate.serve(req, res)
})

lactate.set({
  root:process.cwd(),
  expires:'one day and 12 minutes',
  debug:true
})

app.get('/images/:img', function(req, res) {
  var img = req.params.img
  return lactate.serve('thumbs/'+img, req, res)
})

app.listen(8080)

##The varieties of Lactate experience

In the general case, the Lactate method returns an object with the methods serve set and get, importantly. However, there are more convenient methods exported by Lactate. They follow.

###Serving an individual file

To serve an individual file, use the file method.

  var Lactate = require('lactate')

  app.get('*', function(req, res) {
    return Lactate.file('images/somn.jpg', req, res)
  })

###Namespacing a directory

The dir method allows you to namespace a directory, for convenience.

var Lactate = require('lactate')
var images = Lactate.dir('images', {expires:'one day'})

app.get('/images/:image', function(req, res) {
  return images.serve(req.params.image, req, res)
})

###Middleware

For maximum convenience, you may use the toMiddleware method on directories.

var Lactate = require('lactate')

var images = Lactate.dir('images', {
  expires:'one day'
}).toMiddleware()

app.use(images) //That's it!

You may also pass additional options to the toMiddleware function.

var images = Lactate.dir('images', {
  expires:'one day'
})

var middleware = images.toMiddleware({
  public:'images'
})

app.use(middleware)

##Options

Options can be passed to the initialization function or using the set method.

Setting options

//Passing to initialization function
var lactate = require('lactate').Lactate({
  expires:'two days'
})

//Set method
lactate.set('expires', null)

//Either function accepts (key, value) or an object.

Options available

  • root string

Local directory from which to serve files. By default, the current working directory.

  • public string

Public directory exposed to clients. If set, only requests from /directory will complete.

  • cache boolean

Keep files in-memory. Enabled by default, and no great reason to disable.

  • expires number or string

Pass this function a number (of seconds) or a string and appropriate headers will be set for client-side caching. Lactate comes with expiration defaults, such as 'two days' or '5 years and sixteen days' See Expire for details.

lactate.set('expires', 87500)
//87500 seconds
lactate.set('expires', 'two days')
//172800 seconds
lactate.set'expires', 'five weeks and one minute and ten seconds')
//3024070 seconds
lactate.set('expires', 'one year and 2 months and seven weeks and 16 seconds')
//41050028 seconds

  • debug boolean (optional) number (optional) function (optional)

Debugging in Lactate is level-based (bases: 0, 1). Level 0 logs completed request information, status codes, etc.. Level 1 provides more details along the service. You may override the default debug function (console.log) with your own.

var lactate = require('lactate')({
  debug:true // Will use console.log to debug all events
})

lactate.set('debug', 0, function(level, msg, path, statusCode) {
  /* 
    Captures all level 0 events

    Note however that statusCode arguments are only
    given for level 0 listeners
  */
})

lactate.set('debug', 1, console.log)
lactate.set({debug:false})

##TODO

  • Express middleware
  • Expiration defaults, e.g. 'two days,' 'one month'
  • External (Redis) caching ability

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.