5

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?

edit

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. now I'm not sure what to do now

I tried to lower the version of the node version that I have to 4.0 from 6.0, but still got the same error

5
  • Have you tried the Express example from here or here?
    – mscdex
    Commented Sep 13, 2016 at 22:55
  • on the first link, I get an error as well. points to the map key word and says, unexpected identified. on the 2nd link I don't have any errors but it doesn't try to load seneca as a middleware in express though
    – chip
    Commented Sep 14, 2016 at 1:39
  • basically non of the examples that find around on SO or github are working right now, this is reproduceable.
    – cviejo
    Commented Sep 23, 2016 at 10:22
  • oh man that's not cool at all. im lost with seneca right now. hey man thanks for the answer below, can you please recommend resources so I can really get started with seneca js?
    – chip
    Commented Sep 25, 2016 at 8:36
  • To be honest, to answer the question at some point i just stopped looking around and went to the sources instead. the code in the answer below for example i adapted to your case from one of the seneca-web tests, i figured they are still passing them. Also, once you're set up and have your server up and running, you should be good to go using the existing resources i think -and if not, just post a question :) -.
    – cviejo
    Commented Sep 26, 2016 at 10:04

1 Answer 1

11
+50

Taking a look the main seneca.js file, you'll see that only transport is listed as default plugin (not web):

default_plugins: {
    transport: true
}

The line app.use(seneca.export('web')); is therefore passing undefined to express as a middleware, thus the error you are experiencing.

You have to first explicitely tell seneca to use the web plugin with your express server as parameter. Try this:

var Seneca  = require("seneca");
var Express = require("express");
var Web     = require("seneca-web");

var seneca = Seneca();
var server = Express();

var config = {
    routes:{
        prefix : "/my-api",
        pin: "role:api,cmd:*",
        map:{
            bazinga: {
                GET: true
            }
        }
    }
};

seneca.use(Web, { adapter: "express", context: server })
seneca.act("role:web", config);
seneca.add("role:api,cmd:bazinga", bazinga);

server.listen(3000);


function bazinga(args, done){

    done(null, {
        bar: "Bazinga!"
    });
}

Calling http://localhost:3000/my-api/bazinga in the browser yields {"bar":"Bazinga!"}

1
  • 3
    To further improve this answer. Now seneca requires for express the "Seneca Web Adapter Connect". This answer therefore changes one line to make it work, i.e. : seneca.use(Web, { adapter: require('seneca-web-adapter-express'), context: server }) Don't forget to install npm install --save seneca-web-adapter-express
    – Nick
    Commented Sep 6, 2017 at 10:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.