App.use()需要中间件函数
app.use() requires middleware functions
我使用express和node js为我的应用程序
我的主index.js文件是
var express = require("express");
var app = module.exports = express();
var bodyParser = require('body-parser');
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
var myProject= require("services");
app.use(myProject.callService());
和我的services.js文件
var res = module.exports;
res.callService = function () {
console.log('here ===')
}
但是当我试图从index.js调用这个callService函数时,我得到了一个错误
app.use()需要中间件功能
你能告诉我这里哪里做错了吗
您需要传递一个中间件函数,而不是传递调用端点处理程序的结果。
services.js
// a middleware function gets the request(`req`), response(`res`)
// and a `next` function to pass control to the next handler
// in the pipeline.
exports.callService = function (req, res, next) {
console.log('here ===');
res.status(200).end();
};
index.js
/* setup stuff... */
// notice here we do not call callService, but pass it
// to be called by the express pipeline.
app.use(myProject.callService);
相关文章:
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 什么'在Express中路由时,应用程序级中间件和路由器级中间件之间的区别是什么
- Redux中间件在next()之前更改状态
- 快速的套接字 io 实例/中间件
- 如何使用Express'正确加载图像;s的静态中间件
- Router.use()需要中间件函数,但得到了一个未定义的函数
- 谁在Express中间件中提供next()函数
- Express JS - 对路由和中间件使用匿名函数
- 在redux中使用thunk中间件比使用常规函数作为异步操作创建者有什么好处
- 如何从express中间件函数传递值
- TypeError:app.use()需要中间件函数
- 为什么这段代码作为中间件而不是作为函数工作得很好?
- App.use()需要中间件函数
- 如何在中间件函数中迭代Mongoose模型属性
- 编写koa异步函数中间件时出错
- 在express js的中间件函数中获取会话变量
- TypeError:当向Redux添加中间件时,getState不是一个函数
- react redux中动作函数的目的是什么?它们是否都返回一个动作或函数,然后转到存储/中间件
- 获取一个函数,在继续之前等待post中间件保存完成
- 套接字.IO中间件函数