niccolo m. niccolo m. - 1 year ago 347
Node.js Question

app.use() requires middleware functions app.use( seneca.export('web') );

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')();


prefix: '/my-api',
pin: {role:'api',cmd:'*'},
bazinga: {GET: true}

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

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

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


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

Answer Source

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 = {
        prefix : "/my-api",
        pin: "role:api,cmd:*",
            bazinga: {
                GET: true

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


function bazinga(args, done){

    done(null, {
        bar: "Bazinga!"

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download