1.0.8 • Published 7 years ago

sugo-endpoint-file v1.0.8

Weekly downloads
3
License
Apache-2.0
Repository
github
Last release
7 years ago

sugo-endpoint-file

Build Status npm Version JS Standard

Endpoint to handle files

Requirements

Installation

$ npm install sugo-endpoint-file --save

Usage

Create an instance and apply to sg-server (or sugo-cloud)

/** This is an example to use sugo-endpoint-file */

'use strict'

const sgServer = require('sg-server')

const server = sgServer({
  middlewares: [
    /* ... */
  ],
  endpoints: {
    '/api/my-docs/:filename': require('sugo-endpoint-file')(
      'var/my-docs/', // Directory path to save files
      {
        // Options
      })
  }
})

server.listen(3000)

Then call the api from agent script.

'use strict'

const sugoAgentFile = require('sugo-agent-file')
const co = require('co')

co(function * () {
  let agent = sugoAgentFile('http://my-sever.com/files')

  // Check if server available
  {
    let ok = yield agent.knock() // Send HTTP HEAD request.
    /* ... */
  }

  // Access dynamic files
  {
    // Write
    yield agent.write('my-text-01.txt', 'This is the text.')
    // Read
    let content = yield agent.read('my-text-01.txt')
    console.log(content)
    /* ... */
    // Check
    let exists = yield agent.exists('my-text-01.text')
    if (exists) {
      // Delete
      yield agent.delete('my-text-01.txt')
    }
  }
}).catch((err) => console.error(err))

Signature

sugoEndpointFile(dirname, options) -> function

Endpoint to handle files

Args
NameTypeDefaultDescription
dirnamestringDirectory name of files path
optionsobjectOptional settings.

License

This software is released under the Apache-2.0 License.

Links