Node.js表达:中间件工厂
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Node.js表达:中间件工厂相关的知识,希望对你有一定的参考价值。
我正在尝试使用express在node.js api服务器中编写中间件工厂。
export function argMiddlewareFactory(args: any[])
const middleware = async (
req: Request,
res: Response,
next: NextFunction
) =>
for (let arg of args)
console.log("inside argMiddlewar");
console.log(arg);
if (!req.body.hasOwnProperty(arg))
let errorMessage: string = `Following property is required: $arg`;
res.status(400).send(errorMessage);
next();
;
return middleware;
此函数应接受键数组(后请求所要求,并返回中间件)。例如:
requiredArgs: string[] = ["email","password"];
const credentialMiddleware = argMiddlewareFactory(requiredArgs)
router.use(credentialMiddleware)
问题是当我启动服务器时出现此错误:
TypeError: argMiddlewareFactory is not a function at Object.<anonymous>
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/home/linux/Progetti/Private/ecommerce-cartoleria/Code/product-service/server/build/src/index.js:23:21)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
您能解释为什么吗?
答案
答案下方的评论解决方案,因为这是打字稿,可能是一个文件的编译不正确
以上是关于Node.js表达:中间件工厂的主要内容,如果未能解决你的问题,请参考以下文章