0

module.export にパラメーターを渡す方法を理解しようとしています。これで、テスト用のデモサーバーを作成しました。

ファイル - index.js

var express = require('express');
var check = require('./file');
var app = express();

app.get('/',check.fun1("calling"),(req,res)=>{
    res.send('here i am');
})

app.listen(3000,()=>{
    console.log("Server is up");
})

ミドルウェアチェックで、

module.exports = fun1 = name => {
    return function(req, res, next) {
      console.log(name + " from fun1");
      next();
    };
  };

  module.exports = fun2 = name2 => {
    return function(req, res, next) {
      console.log(name + " from fun1");
      next();
    };
  };

これは機能していませんが、変更すると機能し始めます

fun1 = name => {
  return function(req, res, next) {
    console.log(name + " from fun1");
    next();
  };
};

fun2 = name2 => {
  return function(req, res, next) {
    console.log(name + " from fun1");
    next();
  };
};

module.exports = {
  fun1,
  fun2
};

さて、これはばかげた質問のように見えるかもしれません。それが機能しているのに、なぜ私が尋ねているのか、私の最初のタイプの module.export が機能し始めるように index.js ファイルにどのような変更を加える必要があるのか​​ということです。純粋な好奇心からです

ありがとう

4

1 に答える 1