App.use()需要中间件函数

app.use() requires middleware functions

本文关键字:中间件 函数 use App      更新时间:2023-09-26

我使用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);