senecajs / seneca-web

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

app.use( seneca.export('web') ) throws an error #115

Closed nfantone closed 5 years ago

nfantone commented 8 years ago

From https://github.com/senecajs/senecajs.org/issues/266 Original author: @chessmonster


I'm a noob at seneca and I'm just trying to run sample codes from Developing Microservices with Node js, and it says:

var seneca = require('seneca')();

seneca.add('role:api,cmd:bazinga',function(args,done){
    done(null,{bar:"Bazinga!"});
});

seneca.act('role:web',{use:{
    prefix: '/my-api',
    pin: {role:'api',cmd:'*'},
    map:{
    bazinga: {GET: true}
    }
    }})

var express = require('express');
var app = express();

app.use( seneca.export('web') ); // <<<<<< this line might be the cause 
app.listen(3000);

but Im getting an error that says:

TypeError: app.use() requires middleware functions
    at EventEmitter.use (/home/oem/node_modules/express/lib/application.js:209:11)
    at Object. (/home/oem/Documents/seneca/app.js:7:8)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:974:3

Also I tried to run another sample code copied from the web, I'm sorry I just can find the link. But I just copied, pasted and tried to run and I got the same error. I'm thinking this is more of a setup issue?

I'm still trying to play with this. The way I understand this is that on app.use line, basically I'm just calling the seneca-web module. so what I did was

app.use(require('seneca-web'))

instead of

app.use( seneca.export('web') )

then I ran node app.js, it ran the script with no error in the command. but when tried to access the module from a browser, then I got the error saying util is not found and is pointing to the seneca web file, which actually is from a call from seneca.

tswaters commented 8 years ago

You'll need to:

1) pass context to the config. Docs reference this as Express () but new Router() should work.

2) pull context out with seneca.export('web/context')() -- if this is the app you can call listen on it. If it's a router you can pass it to app.use

tswaters commented 7 years ago

A lot of the docs on getting started and the seneca-attic/getting-started repo were out of date. I'm submitted a few PRs for fixing these to use the latest API.

tswaters commented 5 years ago

Closing old tickets that should be resolved now.