Skip to content

Instantly share code, notes, and snippets.

@tswaters
Last active November 29, 2017 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tswaters/f709900c3f7727e687e22380f7b2bbe4 to your computer and use it in GitHub Desktop.
Save tswaters/f709900c3f7727e687e22380f7b2bbe4 to your computer and use it in GitHub Desktop.
Using Seneca-Web with request$ and response$

Using Seneca-Web with request$ and response$

node web-host.js

In another terminal

node web-client.js

In another terminal

curl -v "localhost:3000/test?body=test&status=400"
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /test?body=test&status=400 HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< X-Powered-By: Express
< Date: Wed, 29 Nov 2017 02:42:26 GMT
< Connection: keep-alive
< Content-Length: 4
<
* Connection #0 to host localhost left intact
test
require('seneca')()
.add('role:service,cmd:test', (msg, cb) => {
const {error, status, body} = msg.args.query
if (error) { return cb(new Error('it really blew up')) }
else { cb(null, {status, body}) }
})
.client({type: 'tcp', pin: 'role:web,routes:*', port: 10010})
.listen({type: 'tcp', pin: 'role:service,cmd:*', port: 10011})
.ready(function () {
this.act('role:web', {routes: [{
pin: 'role:web,cmd:*',
map: {test: {get: true, autoreply: false}}
}]})
})
const express = require('express')
// in the web-host micro service
require('seneca')
.use('seneca-web', {
context: new express.Router(),
adapter: require('seneca-web-adapter-express')
})
.add('role:web,cmd:test', function (msg, cb) {
const {request$, response$} = msg
this.act('role:service,cmd:test', msg, (err, result) => {
if (err) { return cb(err) }
const {status = 200, body = ''} = result
response$.status(status).end(body)
cb()
})
})
.listen({type: 'tcp', pin: 'role:web,routes:*', port: 10010})
.client({type: 'tcp', pin: 'role:service,cmd:*', port: 10011})
.ready(function () {
const app = express()
const router = this.export('web/context')()
app.use('/', router)
app.use((err, req, res, next) => {
if (res.headersSent) { return next(err) }
res.status(500).json(err)
})
app.listen(3000, () => console.log('listening on 3000'))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment