senecajs / seneca-web

Http route mapping for Seneca microservices.
MIT License
76 stars 44 forks source link

Not match hapi now! #136

Closed xfz1987 closed 5 years ago

xfz1987 commented 6 years ago

Lastest, hapi has been updated. it uses async function to start now. So it can not work for hapi. I have to find other ideas to use seneca with hapi. Like this below: -----------server.js--------------- const Hapi = require('hapi') const Routes = require('./routes')

const server = Hapi.server({ host:'localhost', port:8083 });

for(let route of Routes){ server.route(route) }

async function start() { try { await server.start(); } catch (err) { console.log(err); process.exit(1); } console.log('Server running at:', server.info.uri); };

start();

--------------routes/index.js-------------------- const Seneca = require('seneca') const seneca = Seneca()

//服务中心 服务注册 seneca.add('role:math,cmd:sum', (msg, respond) => { respond(null, {answer: (msg.left + msg.right)}) })

const sencaPromise = () => { return new Promise((resolve, reject) => { seneca.act({role: 'math', cmd: 'sum', left: 1, right: 2}, function (err, result) { console.error('得到的值:', err) if (err) { resolve({data: ''}) } else { resolve(result) } }) }) }

const Routes = [ { method: 'GET', path: '/hello', handler: function(request,h) { return sencaPromise() } } ]

module.exports = Routes

/////////////////////////////////////////////////////////////////////////////////////// it can work. please create a well plugin for us. thank you

tswaters commented 6 years ago

If it's using promise-based API to start now, you just need to chain a then and catch to handle the response.... or make it an async function and wrap it in try catch. The current example has...

  .ready(() => {
    var server = seneca.export('web/context')()

    server.start(() => {
      console.log('server started on: ' + server.info.uri)
    })
  })

You should be able to get this to work with...

diff --git a/README.md b/README.md
index d8987f3..c7e1ea7 100644
--- a/README.md
+++ b/README.md
@@ -117,9 +117,12 @@ var seneca = Seneca()
   .ready(() => {
     var server = seneca.export('web/context')()

-    server.start(() => {
-      console.log('server started on: ' + server.info.uri)
-    })
+    server.start()
+      .then(() => console.log('server started on: ' + server.info.uri))
+      .catch(err => {
+        console.error(err)
+        process.exit(1)
+      })
   })

or,

diff --git a/README.md b/README.md
index d8987f3..e2c59ae 100644
--- a/README.md
+++ b/README.md
@@ -114,12 +114,15 @@ var config = {
 var seneca = Seneca()
   .use(Plugin)
   .use(Web, config)
-  .ready(() => {
-    var server = seneca.export('web/context')()
-
-    server.start(() => {
-      console.log('server started on: ' + server.info.uri)
-    })
+  .ready(async () => {
+    try {
+      var server = seneca.export('web/context')()
+      await server.start()
+      console.log('server started on: ' + server.info.uri)
+    } catch (err) {
+      console.error(err)
+      process.exit(1)
+    }
   })

To be honest, I don't use HAPI at all. Assuming they still support the handler (request, h) signature, seneca-web-adapter-hapi should continue to work.

tswaters commented 5 years ago

Closing old tickets that should be resolved now.