gpt4 book ai didi

node.js - app.use() 需要中间件函数 app.use( seneca.export ('web' ) );

转载 作者:搜寻专家 更新时间:2023-10-31 22:37:43 30 4
gpt4 key购买 nike

我是 seneca 的菜鸟,我只是想运行 Developing Microservices with Node js 中的示例代码,它说:

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

但我收到一条错误消息:

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

代替

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

然后我运行 node app.js,它运行了脚本,命令中没有错误。但是当试图从浏览器访问模块时,我收到错误消息,指出未找到 util 并指向 seneca web 文件,该文件实际上来自 seneca 的调用。现在我不确定现在该做什么

我试图将我必须的 Node 版本从 6.0 降低到 4.0,但仍然出现相同的错误

最佳答案

看看主要seneca.js file ,您会看到只有 transport 被列为默认插件(不是 web):

default_plugins: {
transport: true
}

app.use(seneca.export('web')); 行因此传递 undefined 以表示为中间件,因此您遇到的错误。

您必须首先明确告诉 seneca 使用 web 插件并将您的 express 服务器作为参数。试试这个:

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!"
});
}

调用 http://localhost:3000/my-api/bazinga在浏览器中产生{"bar":"Bazinga!"}

关于node.js - app.use() 需要中间件函数 app.use( seneca.export ('web' ) );,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39480013/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com