Giter VIP home page Giter VIP logo

seneca-web-adapter-koa2's Introduction

Seneca

seneca-web-adapter-koa2

npm version Build Status Coverage Status Dependency Status

  • Node: 6.x, 8.x
  • Seneca: 1.x - 3.x

This is an adapter for seneca-web using koa/koa-routes.

Installation

Peer dependencies have been specified for koa, koa-router and seneca-web They need to be present for this to work at all.

npm install --save koa@2
npm install --save koa-router@7
npm install --save seneca
npm install --save seneca-web
npm install --save seneca-web-adapter-koa2

Usage

Please refer to the seneca-web documentation on how to load routes.

You can require this module as the adapter when using the SenecaWeb plugin

const Seneca = require('seneca')
const SenecaWeb = require('seneca-web')
const Koa = require('koa')
const Router = require('koa-router')
const app = Koa()
const seneca = Seneca()
seneca.use(SenecaWeb, {
  context: Router(),
  adapter: require('seneca-web-adapter-koa2')
})
seneca.ready(() => {
  app.use(
    seneca
      .export('web/context')()
      .routes()
  )
})

Note

Most of the seneca-web documents refer to passing the application as the context parameter. Doing this with koa will result in unexpected explosions. Koa Router must be used.

Contributing

The Senecajs org encourage open participation. If you feel you can help in any way, be it with documentation, examples, extra testing, or new features please get in touch.

License

Copyright (c) 2016-2017, Tyler Waters. Licensed under MIT.

seneca-web-adapter-koa2's People

Contributors

amencionna avatar tswaters avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

seneca-web-adapter-koa2's Issues

Improve Readme quality (match main repo)

There are bits of the README missing to link back to the License and contribution statements. There are badges missing too. This should be done for all adapters to maintain parity across the org.

ctx.request.querystring to ctx.request.query sa that return a JSON

if (ctx.req.method === 'POST') {
          body = await Parse(ctx)
        }

        **const query = ctx.request.querystring**

        const payload = {
          request$: ctx.request,
          response$: ctx.response,
          args: {body, query}
        }

req:http://127.0.0.1:3000/todo/edit?left=1&right=2
return:left=1&right=2

if (ctx.req.method === 'POST') {
          body = await Parse(ctx)
        }

        **const query = ctx.request.query**

        const payload = {
          request$: ctx.request,
          response$: ctx.response,
          args: {body, query}
        }

req:http://127.0.0.1:3000/todo/edit?left=1&right=2
return:{ left: '1', right: '2' }

^ SyntaxError: Unexpected token

const Seneca = require('seneca')
const SenecaWeb = require('seneca-web')
const Koa = require('koa')
const Router = require('koa-router')
const app = Koa()
const seneca = Seneca()
seneca.use(SenecaWeb, {
  context: Router(),
  adapter: require('seneca-web-adapter-koa2')
})
seneca.ready(() => {
  app.use(seneca.export('web/context')().routes())
`})`

Run the above code to get an error

context[method.toLowerCase()](route.path, async (ctx, next) => {
                                                      ^
SyntaxError: Unexpected token (
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\Users\Search You\WebstormProjects\node-ms-user\index.js:13:14)
    at Module._compile (module.js:570:32)

payload lose params

when I use seneca+koa , try Web Server Integration(http://senecajs.org/getting-started/) , run failure.
but same plugin code run with seneca-web-adapter-express is OK.
my plugin code :

module.exports = function api(options) {

  var valid_ops = { sum:'sum', product:'product' }

  this.add('role:api,path:calculate', function (msg, respond) {
    console.log("msg",msg);

    // failure this line
    var operation = msg.args.params.operation

    var left = msg.args.query.left
    var right = msg.args.query.right
    this.act('role:math', {
      cmd:   valid_ops[operation],
    //   cmd:   valid_ops['sum'],
      left:  left,
      right: right,
    }, respond)
  })


  this.add('init:api', function (msg, respond) {
    this.act(
        'role:web',{
            routes:[{
                prefix: '/api',
                pin: 'role:api,path:*',
                map: {
                    calculate: {
                        GET:true,
                        suffix:'/:operation'
                    }
                }
            }]
        },
        respond
    )
  })

}

I found msg.args.params is null,check the seneca-web-adapter-koa2 sourcecode,because not set params prop,so I modify code "seneca-web-adapter-koa2.js" ,my code run OK.

        let params = ctx.params;

        const payload = {
          request$: ctx.request,
          response$: ctx.response,
          args: {body, query,params}
        }

the post &put request does not work

my route map

module.exports = [
    {
        prefix: 'v1/user',
        pin: 'area:user,action:*',
        map: {
            create: {
                POST: true
            },
            list: {
                GET: true,
                POST: true
            },
        }
    }
];

when i modify the code like this ,it is work ;please check it

        if (['POST', 'PUT'].indexOf(ctx.req.method) > -1) {
          body = await Parse(ctx)
        }

to

if (['POST', 'PUT'].indexOf(ctx.request.method) > -1) {
          body = Object.assign({}, ctx.request.body)
        }

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.