Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2 #5

Closed
tunnckoCore opened this issue Aug 6, 2017 · 1 comment
Closed

v2 #5

tunnckoCore opened this issue Aug 6, 2017 · 1 comment

Comments

@tunnckoCore
Copy link
Owner

just for backup, soon i'll scaffold it

/*!
 * koa-better-serve <https://github.com/tunnckoCore/koa-better-serve>
 *
 * Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
 * Released under the MIT license.
 */

'use strict'

var send = require('koa-send')
var kindOf = require('kind-of')

/**
 * > Serving `dir` of files and folders, when request
 * url (`ctx.url`) match to `pathname`. All behind
 * the scenes is just using [koa-send][].
 * **Hint:** use [koa-convert][] to use it for [koa][] v1.
 *
 * **Example**
 *
 * ```js
 * const serve = require('koa-better-serve')
 * const Koa = require('koa')
 * const app = new Koa()
 *
 * app.use(serve('./uploads/images', '/images'))
 * app.listen(4290)
 * ```
 *
 * @param  {String|Buffer} root folder to serve
 * @param  {String|RegExp} pathname path to match, can be regex
 * @param  {Object} options optional, passed directly to [koa-send][]
 * @return {Function} [koa][] plugin which returns `Promise` when called
 * @api public
 */
module.exports = function koaBetterServe(root, pathname, options) {
  if (kindOf(root) !== 'string') {
    throw new TypeError('koa-send-serve: expect `root` to be string')
  }
  if (kindOf(pathname) === 'object') {
    options = pathname
    pathname = '/'
  }

  const type = kindOf(pathname)
  const opts = Object.assign({ root }, options)
  let regex = type === 'regexp' ? pathname : null

  if (type === 'string') {
    pathname = pathname[0] === '^' ? pathname.slice(1) : pathname
    pathname = pathname[0] === '/' ? pathname : '/' + pathname
    regex = new RegExp('^' + pathname)
  }

  return function(ctx, next) {
    if (regex.test(ctx.path)) {
      const path = require('path')
      const fp = ctx.path.replace(pathname, '')
      const filepath = path.join(opts.root, fp)
      return send(ctx, filepath, opts)
    }
  }
}
@tunnckoCore tunnckoCore reopened this Aug 6, 2017
@tunnckoCore tunnckoCore reopened this Aug 6, 2017
@tunnckoCore tunnckoCore reopened this Aug 6, 2017
@tunnckoCore tunnckoCore reopened this Aug 6, 2017
@tunnckoCore tunnckoCore reopened this Aug 6, 2017
tunnckoCore pushed a commit that referenced this issue Aug 6, 2017
BREAKING CHANGE: rewrite from scratch; fix bugs; update build process, npm scripts & boilerplate stuff

fixes #4, resolves #5

BREAKING CHANGE: rewrite from scratch; fix bugs; update build process, npm scripts & boilerplate stuff
tunnckoCore pushed a commit that referenced this issue Aug 6, 2017
BREAKING CHANGE: rewrite from scratch; fix bugs; update build process, npm scripts & boilerplate stuff

fixes #4, resolves #5

BREAKING CHANGE: rewrite from scratch; fix bugs; update build process, npm scripts & boilerplate stuff
tunnckoCore pushed a commit that referenced this issue Aug 6, 2017
BREAKING CHANGE: rewrite from scratch; fix bugs; update build process, npm scripts & boilerplate stuff

fixes #4, resolves #5

BREAKING CHANGE: rewrite from scratch; fix bugs; update build process, npm scripts & boilerplate stuff
tunnckoCore pushed a commit that referenced this issue Aug 6, 2017
fixes #4, resolves #5

BREAKING CHANGE: rewrite from scratch; fix bugs; update build process, npm scripts & boilerplate stuff
@tunnckoCore
Copy link
Owner Author

Version 2.0.0 has been published to NPM. The full release note can be found at koa-better-serve/releases/tag/v2.0.0.

Tip: safely upgrade dependency koa-better-serve in your project using next-update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant