How can I use body-parser with LoopBack?

前端 未结 8 703
别那么骄傲
别那么骄傲 2020-12-24 13:56

I see that LoopBack has the Express 3.x middleware built-in. Indeed, body-parser is in loopback/node_modules. But I cannot figure out how to use it as middlewar

8条回答
  •  清歌不尽
    2020-12-24 14:37

    I'm using loopback 2.14.0:

    To make use of the body-parser in your custom bootscript routes you should only need to:

    1) install body-parser npm install body-parser --save

    2) Register the the module in middleware.json

    "parse": {
    "body-parser#json": {},
    "body-parser#urlencoded": {"params": { "extended": true }}
    },
    

    There is no need to require the parser setup in server.js, loopback does this for you when you register the middleware.

    Please note body parser is now installed in your source "node_modules" directory as well as in the loopback modules directory.

    If at all possible try register custom remote methods as described in the loopback documentation.

    Registering routes this way gives you access to loopback's body-parser out of the box and is the 'cleanest' implementation.

提交回复
热议问题