How to add body-parser to Inversify's Express Adapter?

2 weeks ago 15
ARTICLE AD BOX

I can't figure out how to add body-parser to Inversify's new built-in express adapter. I currently have a very simple setup (basically right from the docs):

import { Container } from 'inversify'; import {InversifyExpressHttpAdapter} from '@inversifyjs/http-express'; import express from "express"; const container: Container = new Container(); // Bind Controllers to container const adapter: InversifyExpressHttpAdapter = new InversifyExpressHttpAdapter( container, ); // Presumably, I need to add body-parser here somehow const application: express.Application = await adapter.build(); application.listen(3000);

adapter does not appear to expose any way of affecting the underlying app prior to the build happening. If I look back on previous examples that used the (now deprecated) inversify-express-utils, library, it was done via a setConfig method:

let container = new Container(); container.load(module); let server = new InversifyExpressServer(container); server.setConfig((app) => { // add body parser app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); });

Looking at the new adapter, it exposes a few methods for adding stuff like middleware and pipes, but not for modifying the express app.

How can I add body-parser when using the new built-in way?


Just in case this is an XY problem and adding body-parser is unnecessary, I'm trying to add body-parser because the body of requests is undefined here:

// In a controller @Post('/') public async createAccount(@Body() body: { username: string, password: string }) { await this.accountService.createAccount(body.username, body.password); }

The controller method is entered, so I know that that's set up right, but body is undefined which obviously causes a failure.

Read Entire Article