Giter VIP home page Giter VIP logo

koa-better-serve's Introduction

koa-better-serve npm version github release mit License NPM Downloads Weekly NPM Downloads Total

Small, simple and correct serving of files, using koa-send - nothing more

You might also be interested in koa-better-router.

Quality Assurance πŸ’―

Code Climate Code Style Standard Linux Build Code Coverage Dependencies Status Renovate App Status

If you have any how-to kind of questions, please read Code of Conduct and join the chat room or open an issue.
You may also read the Contributing Guide. There, beside "How to contribute?", we describe everything stated by the badges.

tunnckoCore support Code Format Prettier node security status conventional Commits semantic release Node Version Required

Table of Contents

(TOC generated by verb using markdown-toc)

Install

This project requires Node.js v7 and above. Use npm to install it.

$ npm install koa-better-serve

API

Review carefully the provided examples and the working tests.

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.

Params

  • root {String|Buffer}: folder to serve
  • pathname {String|RegExp}: path to match, can be regex
  • options {Object}: optional, passed directly to koa-send
  • returns {Function}

Example

const serve = require('koa-better-serve')
const Koa = require('koa')
const app = new Koa()

app.use(serve('./uploads/images', '/images'))
app.listen(4290)

Related

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Please read the Contributing Guide and Code of Conduct documents for advices.

Author

License

Copyright Β© 2016-2017, Charlike Mike Reagent. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on August 15, 2017.
Project scaffolded using charlike-cli.

koa-better-serve's People

Contributors

artptr avatar renovate-bot avatar renovate[bot] avatar tunnckocore avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

koa-better-serve's Issues

Weekly Digest (24 February, 2019 - 3 March, 2019)

Here's the Weekly Digest for tunnckoCoreLabs/koa-better-serve:


ISSUES

Last week, no issues were created.


PULL REQUESTS

Last week, no pull requests were created, updated or merged.


COMMITS

Last week there were no commits.


CONTRIBUTORS

Last week there were no contributors.


STARGAZERS

Last week there were no stargazers.


RELEASES

Last week there were no releases.


That's all for last week, please πŸ‘€ Watch and ⭐ Star the repository tunnckoCoreLabs/koa-better-serve to receive next weekly updates. πŸ˜ƒ

You can also view all Weekly Digests by clicking here.

Your Weekly Digest bot. πŸ“†

Weekly Digest (3 March, 2019 - 10 March, 2019)

Here's the Weekly Digest for tunnckoCoreLabs/koa-better-serve:


ISSUES

Last week, no issues were created.


PULL REQUESTS

Last week, no pull requests were created, updated or merged.


COMMITS

Last week there were no commits.


CONTRIBUTORS

Last week there were no contributors.


STARGAZERS

Last week there were no stargazers.


RELEASES

Last week there were no releases.


That's all for last week, please πŸ‘€ Watch and ⭐ Star the repository tunnckoCoreLabs/koa-better-serve to receive next weekly updates. πŸ˜ƒ

You can also view all Weekly Digests by clicking here.

Your Weekly Digest bot. πŸ“†

Weekly Digest (10 February, 2019 - 17 February, 2019)

Here's the Weekly Digest for tunnckoCoreLabs/koa-better-serve:


ISSUES

Last week, no issues were created.


PULL REQUESTS

Last week, no pull requests were created, updated or merged.


COMMITS

Last week there were no commits.


CONTRIBUTORS

Last week there were no contributors.


STARGAZERS

Last week there were no stargazers.


RELEASES

Last week there were no releases.


That's all for last week, please πŸ‘€ Watch and ⭐ Star the repository tunnckoCoreLabs/koa-better-serve to receive next weekly updates. πŸ˜ƒ

You can also view all Weekly Digests by clicking here.

Your Weekly Digest bot. πŸ“†

Weekly Digest (8 February, 2019 - 15 February, 2019)

Here's the Weekly Digest for tunnckoCoreLabs/koa-better-serve:


ISSUES

Last week, no issues were created.


PULL REQUESTS

Last week, no pull requests were created, updated or merged.


COMMITS

Last week there were no commits.


CONTRIBUTORS

Last week there were no contributors.


STARGAZERS

Last week there were no stargazers.


RELEASES

Last week there were no releases.


That's all for last week, please πŸ‘€ Watch and ⭐ Star the repository tunnckoCoreLabs/koa-better-serve to receive next weekly updates. πŸ˜ƒ

You can also view all Weekly Digests by clicking here.

Your Weekly Digest bot. πŸ“†

forbidden error

ForbiddenError: Forbidden at resolvePath (/Users/yuri/work/library_be/node_modules/resolve-path/index.js:82:11) at /Users/yuri/work/library_be/node_modules/koa-send/index.js:69:12 at Generator.next (<anonymous>) at onFulfilled (/Users/yuri/work/library_be/node_modules/co/index.js:65:19) at /Users/yuri/work/library_be/node_modules/co/index.js:54:5 at co (/Users/yuri/work/library_be/node_modules/co/index.js:50:10) at send (/Users/yuri/work/library_be/node_modules/koa-send/index.js:36:10) at /Users/yuri/work/library_be/node_modules/koa-better-serve/index.js:63:16 at process._tickCallback (internal/process/next_tick.js:109:7

Can't make it work

Hi

I was trying to use the package because it looks as simple as I would like it to. Unfortunately I can't make it work with any setup. Here are the details:

node version: 8.2.1
koa version: ^2.3.0

setup:

app.use(serve('../build'))

// also tried
app.use(serve('../build', ''))
app.use(serve('../build', '/'))
app.use(serve('../build', '/build'))
app.use(serve('./index.html', '/'))
app.use(serve('./index.html'))

error I got:

{ AssertionError [ERR_ASSERTION]: pathname required                                                                                                  [7/755]
    at /home/username/folder/node_modules/koa-send/index.js:39:5
    at Generator.next (<anonymous>)
    at onFulfilled (/home/username/folder/supplies/node_modules/co/index.js:65:19)
    at /home/username/folder/node_modules/co/index.js:54:5
    at Promise (<anonymous>)
    at co (/home/username/folder/node_modules/co/index.js:50:10)
    at send (/home/username/folder/node_modules/koa-send/index.js:36:10)
    at /home/username/folder/node_modules/koa-better-serve/index.js:69:16
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
  generatedMessage: false,
  name: 'AssertionError [ERR_ASSERTION]',
  code: 'ERR_ASSERTION',
  actual: '',
  expected: true,
  operator: '==' } { request: 
   { method: 'GET',
     url: '/',
     header: 
      { host: 'localhost:3001',
        connection: 'keep-alive',
        'cache-control': 'max-age=0',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36',
        accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        dnt: '1',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4' } },
  response: { status: 404, message: 'Not Found', header: {} },
  app: { subdomainOffset: 2, proxy: false, env: 'production' },
  originalUrl: '/',
  req: '<original node req>',
  res: '<original node res>',
  socket: '<original node socket>' }

v2

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)
    }
  }
}

luck of documentation

Hi!

I tryed to config server for 3 hours but with result ((
I need send files in request "/assets/img1.jpg" my assets files are in the folder "../../dist"

How to configure server?
Help me please

Weekly Digest (17 February, 2019 - 24 February, 2019)

Here's the Weekly Digest for tunnckoCoreLabs/koa-better-serve:


ISSUES

Last week, no issues were created.


PULL REQUESTS

Last week, no pull requests were created, updated or merged.


COMMITS

Last week there were no commits.


CONTRIBUTORS

Last week there were no contributors.


STARGAZERS

Last week there were no stargazers.


RELEASES

Last week there were no releases.


That's all for last week, please πŸ‘€ Watch and ⭐ Star the repository tunnckoCoreLabs/koa-better-serve to receive next weekly updates. πŸ˜ƒ

You can also view all Weekly Digests by clicking here.

Your Weekly Digest bot. πŸ“†

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.