项目作者: realglobe-Inc

项目描述 :
Endpoint to handle files
高级语言: JavaScript
项目地址: git://github.com/realglobe-Inc/sugo-endpoint-file.git
创建时间: 2016-06-29T02:57:43Z
项目社区:https://github.com/realglobe-Inc/sugo-endpoint-file

开源协议:Apache License 2.0

下载


sugo-endpoint-file

Build Status
npm Version
JS Standard

Endpoint to handle files

Requirements


Node.js


NPM

Installation

  1. $ npm install sugo-endpoint-file --save

Usage

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

  1. /** This is an example to use sugo-endpoint-file */
  2. 'use strict'
  3. const sgServer = require('sg-server')
  4. const server = sgServer({
  5. middlewares: [
  6. /* ... */
  7. ],
  8. endpoints: {
  9. '/api/my-docs/:filename': require('sugo-endpoint-file')(
  10. 'var/my-docs/', // Directory path to save files
  11. {
  12. // Options
  13. })
  14. }
  15. })
  16. server.listen(3000)

Then call the api from agent script.

  1. 'use strict'
  2. const sugoAgentFile = require('sugo-agent-file')
  3. const co = require('co')
  4. co(function * () {
  5. let agent = sugoAgentFile('http://my-sever.com/files')
  6. // Check if server available
  7. {
  8. let ok = yield agent.knock() // Send HTTP HEAD request.
  9. /* ... */
  10. }
  11. // Access dynamic files
  12. {
  13. // Write
  14. yield agent.write('my-text-01.txt', 'This is the text.')
  15. // Read
  16. let content = yield agent.read('my-text-01.txt')
  17. console.log(content)
  18. /* ... */
  19. // Check
  20. let exists = yield agent.exists('my-text-01.text')
  21. if (exists) {
  22. // Delete
  23. yield agent.delete('my-text-01.txt')
  24. }
  25. }
  26. }).catch((err) => console.error(err))

Signature

sugoEndpointFile(dirname, options) -> function

Endpoint to handle files

Args
Name Type Default Description
dirname string Directory name of files path
options object Optional settings.

License

This software is released under the Apache-2.0 License.