Code Monkey home page Code Monkey logo

seneca-web-adapter-express's Introduction

Seneca

seneca-web-adapter-express

npm version Build Status Coverage Status Dependency Status

This is an adapter for seneca-web using express.

Installation

A peer dependency has been specified for seneca-web And of course express must be present for this to work at all.

npm install --save express
npm install --save seneca-web
npm install --save seneca-web-adapter-express

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 Express = require('express')
const seneca = Seneca()
seneca.use(SenecaWeb, {
  context: Express(),
  adapter: require('seneca-web-adapter-express'),
  // defaults --
  options: {
    parseBody: true,
    includeRequest: true,
    includeResponse: true
  }
})
seneca.ready(() => {
  const app = seneca.export('web/context')()
  app.listen(3000)
})

Additional Options

  • parseBody attempt to parse request body into req.body
  • includeRequest include request$ with payload (req)
  • includeResponse include response$ with payload (res)

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) 2013 - 2017, Richard Rodger and other contributors. Licensed under MIT.

seneca-web-adapter-express's People

Contributors

amencionna avatar tswaters avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

seneca-web-adapter-express's Issues

add tests for auth/secure routes

Once this has broken out of the main seneca-web, the test coverage is below the threshold... this is mostly due to a lack of tests around using passport for auth/secured routes.

update docco@latest

Got a security vulnerability warning from github - need to reinstall docco@latest and persist to package-json. It's only a devDependency - and I don't think we use the annotate script which uses it.... either way, should update it.

New release - 1.0.2

  • updated error handling to pass back to the application
  • update peer dependency to work with seneca-web 2.0

Why does my interface need 500ms after using this adapter

const express = require('express');
const Seneca = require('seneca');
const SenecaWeb = require('seneca-web');
const compression = require('compression');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const routes = require('./routes');
const adapter = require('seneca-web-adapter-express');
const IS_DEBUG = process.env.NODE_ENV !== 'production';
const ServiceConfs = IS_DEBUG ? require('../configs/service.dev.json') : require('../configs/service.prod.json');

const seneca = Seneca()
  .test('print')
  .use(SenecaWeb, {
    context: (() => {
      const app = express();
      app.use(bodyParser.json());
      app.use(compression());
      app.use(bodyParser.urlencoded({ extended: false }))
      app.use(cookieParser());
      return app;
    })(),
    adapter,
    routes,
  })
  .use("entity", {
    mem_store: false
  })
  .client(ServiceConfs['seneca-srv1'])
  .client(ServiceConfs['order-srv'])
  .ready(async() => {
    const app = seneca.export('web/context')();
    const port = process.env.PORT;
    app.listen(port, () => {
      console.log(`Api Server listen on: ${port}`);
    });
  });

image

Express server crashes when seneca plugin returns an error

  1. Checkout the senecajs/seneca-web repo:
  2. Edit the file docs/examples/common/plugin.js
    Change this
  seneca.add('role:todo,cmd:list', (msg, done) => {
    done(null, {ok: true})
  })

to this

  seneca.add('role:todo,cmd:list', (msg, done) => {
    done(new Error('No todo lists found'), {ok: false})
  })
  1. Run the server node docs/examples/using-express.js
  2. Open your browser to http://localhost:4000/todo/list

The result is a FATAL error:

Seneca Fatal Error
==================

Message: seneca: Action cmd:list,role:todo failed: .

Code: act_execute

Details: { message: '',
  pattern: 'cmd:list,role:todo',
  fn: [Function],
  cb: [Function],
  instance: 'Seneca/mvacw49oa56r/1478047256740/51658/3.2.1/-',
  'orig$':
   Error
       at Seneca.seneca.add (/open-source/seneca-web/docs/examples/common/plugin.js:7:10)
       at execute_action (/open-source/seneca-web/node_modules/seneca/seneca.js:1015:20)
       at Object.act_fn [as fn] (/open-source/seneca-web/node_modules/seneca/seneca.js:911:11)
       at Immediate.processor (/open-source/seneca-web/node_modules/gate-executor/gate-executor.js:131:14)
       at runCallback (timers.js:637:20)
       at tryOnImmediate (timers.js:610:5)
       at processImmediate [as _immediateCallback] (timers.js:582:5),
  'message$': '',
  plugin: {} }

Stack:
    at Seneca.seneca.add (/open-source/seneca-web/docs/examples/common/plugin.js:7:10)
    at execute_action (/open-source/seneca-web/node_modules/seneca/seneca.js:1015:20)
    at Object.act_fn [as fn] (/open-source/seneca-web/node_modules/seneca/seneca.js:911:11)
    at Immediate.processor (/open-source/seneca-web/node_modules/gate-executor/gate-executor.js:131:14)
    at runCallback (timers.js:637:20)
    at tryOnImmediate (timers.js:610:5)
    at processImmediate [as _immediateCallback] (timers.js:582:5)

Instance: Seneca/mvacw49oa56r/1478047256740/51658/3.2.1/-
  ALL ERRORS FATAL: action called with argument fatal$:true (probably a plugin init error, or using a plugin seneca instance, see senecajs.org/fatal.html)
    at Seneca.act_done (/open-source/seneca-web/node_modules/seneca/seneca.js:1054:29)

When: 2016-11-02T00:41:02.825Z

Log: {kind:null,plugin:seneca,tag:3.2.1,id:mvacw49oa56r/1478047256740/51658/3.2.1/-,code:act_execute,notice:seneca:

Node:
  { http_parser: '2.7.0', node: '6.9.1', v8: '5.1.281.84', uv: '1.9.1', zlib: '1.2.8', ares: '1.10.1-DEV', icu: '57.1', modules: '48', openssl: '1.0.2j' },
  { debug: false, uv: true, ipv6: true, tls_npn: true, tls_alpn: true, tls_sni: true, tls_ocsp: true, tls: true },
  [ 'Binding contextify', 'Binding natives', 'NativeModule events', 'NativeModule util', 'Binding uv', 'NativeModule buffer', 'Binding buffer', 'Binding util', 'NativeModule internal/util', 'NativeModule timers', 'Binding timer_wrap', 'NativeModule internal/linkedlist', 'NativeModule assert', 'NativeModule internal/process', 'Binding config', 'NativeModule internal/process/warning', 'NativeModule internal/process/next_tick', 'NativeModule internal/process/promises', 'NativeModule internal/process/stdio', 'Binding constants', 'NativeModule path', 'NativeModule module', 'NativeModule internal/module', 'NativeModule vm', 'NativeModule fs', 'Binding fs', 'NativeModule stream', 'NativeModule _stream_readable', 'NativeModule internal/streams/BufferList', 'NativeModule _stream_writable', 'NativeModule _stream_duplex', 'NativeModule _stream_transform', 'NativeModule _stream_passthrough', 'Binding fs_event_wrap', 'NativeModule tty', 'NativeModule net', 'NativeModule internal/net', 'Binding cares_wrap', 'Binding tty_wrap', 'Binding tcp_wrap', 'Binding pipe_wrap', 'Binding stream_wrap', 'Binding signal_wrap', 'NativeModule http', 'NativeModule _http_incoming', 'NativeModule _http_common', 'Binding http_parser', 'NativeModule internal/freelist', 'NativeModule _http_outgoing', 'NativeModule _http_server', 'NativeModule _http_agent', 'NativeModule _http_client', 'NativeModule url', 'Binding icu', 'NativeModule querystring', 'NativeModule crypto', 'Binding crypto', 'NativeModule internal/streams/lazy_transform', 'NativeModule string_decoder', 'NativeModule console', 'NativeModule os', 'Binding os', 'NativeModule https', 'NativeModule tls', 'NativeModule _tls_common', 'NativeModule _tls_wrap', 'NativeModule _stream_wrap', 'Binding js_stream', 'Binding tls_wrap', 'NativeModule _tls_legacy', 'NativeModule cluster', 'NativeModule dgram', 'Binding udp_wrap', 'NativeModule child_process', 'Binding spawn_sync', 'NativeModule internal/child_process', 'Binding process_wrap', 'NativeModule internal/socket_list', 'NativeModule internal/cluster' ]

Process:
  pid=51658, arch=x64, platform=darwin,
  path=/Users/djensen/.nvm/versions/node/v6.9.1/bin/node,
  argv=[ '/Users/djensen/.nvm/versions/node/v6.9.1/bin/node',  '/open-source/seneca-web/docs/examples/using-express.js' ],
  env={ MANPATH: '/Users/djensen/.nvm/versions/node/v6.9.1/share/man:/usr/local/git/share/man:/usr/local/share/man:/usr/share/man:/opt/X11/share/man:/Library/Developer/CommandLineTools/usr/share/man',  TERM_PROGRAM: 'iTerm.app',  NVM_CD_FLAGS: '-q',  TERM: 'xterm-256color',  SHELL: '/bin/zsh',  TMPDIR: '/var/folders/3g/75g3h8cj68z3ms77nqlnr3m80000gn/T/',  NVM_PATH: '/Users/djensen/.nvm/versions/node/v6.9.1/lib/node',  Apple_PubSub_Socket_Render: '/private/tmp/com.apple.launchd.XF53bIWSkI/Render',  TERM_PROGRAM_VERSION: '2.9.20160430-nightly',  TERM_SESSION_ID: 'w0t0p3:35920AB5-A1B7-4FBC-A797-34029A46ABE4',  ZSH: '/Users/djensen/.oh-my-zsh',  NVM_DIR: '/Users/djensen/.nvm',  USER: 'djensen',  SSH_AUTH_SOCK: '/private/tmp/com.apple.launchd.dpqCJG7BSL/Listeners',  __CF_USER_TEXT_ENCODING: '0x1F5:0x0:0x0',  PAGER: 'less',  LSCOLORS: 'exfxcxdxbxegedabagacad',  PATH: '/Users/djensen/.nvm/versions/node/v6.9.1/bin:/Users/djensen/bin:/usr/local/git/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Users/djensen/Library/Android/sdk/platform-tools',  _: '/Users/djensen/.nvm/versions/node/v6.9.1/bin/node',  NVM_NODEJS_ORG_MIRROR: 'https://nodejs.org/dist',  PWD: '/open-source/seneca-web',  LANG: 'en_US.UTF-8',  ITERM_PROFILE: 'Default',  XPC_FLAGS: '0x0',  XPC_SERVICE_NAME: '0',  SHLVL: '2',  HOME: '/Users/djensen',  COLORFGBG: '7;0',  ITERM_SESSION_ID: 'w0t0p3:35920AB5-A1B7-4FBC-A797-34029A46ABE4',  LESS: '-R',  LOGNAME: 'djensen',  LC_CTYPE: 'en_US.UTF-8',  NVM_BIN: '/Users/djensen/.nvm/versions/node/v6.9.1/bin',  NVM_IOJS_ORG_MIRROR: 'https://iojs.org/dist',  DISPLAY: '/private/tmp/com.apple.launchd.X4OT83Ktcd/org.macosforge.xquartz:0' }




SENECA TERMINATED at 2016-11-02T00:41:02.835Z. See above for error report.

To make sure I wasn't misunderstanding some basic concept I created a small standalone test:

var Seneca = require('seneca');
var seneca = Seneca();
seneca
  .use('./common/plugin')
  .ready( () => {
    seneca.act('role:todo,cmd:edit', {}, (err, reply) => console.log(err ? 'error' : 'no error'));
  });

This, as expected, prints out "error" it runs.

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.