节点 js 干净的架构不能 return 响应

Node js clean architecture cant return response

我正在尝试为我的节点 js rest 实现一个干净的架构 API 我学习了一个教程 Using Clean Architecture for Microservice APIs in Node.js with MongoDB and Express ...我的目标是调整 HTTP 请求并调用我的控制器作为函数和 return 响应

server.js

import express from 'express';
import dotenv from 'dotenv';
import bodyParser from 'body-parser';
import httpAdapter from './http_adapter';
import register from './register';

const app = express();
app.use(bodyParser.json());

dotenv.config();

app.post('/account/register', httpAdapter(register));

const port = 5000;
app.listen(port, () => console.log(`server running on port  ${port}`));

http_adpter.js

export default function httpAdapter(controller) {
    return (req, res) => {
        const httpRequest = {
            body: req.body,
            query: req.query,
            params: req.params,
            ip: req.ip,
            method: req.method,
            path: req.path,
            headers: {
                'Content-Type': req.get('Content-Type'),
                Referer: req.get('referer'),
                'User-Agent': req.get('User-Agent'),
            },
        };
        controller(httpRequest)
            .then((httpResponse) => {
                if (httpResponse.headers) {
                    res.set(httpResponse.headers);
                }
                res.type('json');
                res.status(httpResponse.statusCode).send(httpResponse.body);
            })
            .catch((e) =>
                res.status(500).send({ error: 'An unkown error occurred.' })
            );
    };
}

register.js

export default function register(httpRequest) {
    return () => {
        try {
            const user = httpRequest.body;
            console.log(user);
            return {
                headers: {
                    'Content-Type': 'application/json',
                },
                statusCode: 201,
                body: { user },
            };
        } catch (e) {
            // TODO: Error logging
            console.log(e);
            return {
                headers: {
                    'Content-Type': 'application/json',
                },
                statusCode: 400,
                body: {
                    error: e.message,
                },
            };
        }
    };
}

错误

[nodemon] restarting due to changes...[nodemon] starting `babel-node ./src/server.js`
server running on port  5000
TypeError: controller(...).then is not a function
    at C:\Users\tukuyoma\Desktop\odemru\server\src\/http_adapter.js:17:5
    at Layer.handle [as handle_request] (C:\Users\tukuyoma\Desktop\odemru\server\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\tukuyoma\Desktop\odemru\server\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\tukuyoma\Desktop\odemru\server\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\tukuyoma\Desktop\odemru\server\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\tukuyoma\Desktop\odemru\server\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\tukuyoma\Desktop\odemru\server\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\tukuyoma\Desktop\odemru\server\node_modules\express\lib\router\index.js:275:10)
    at C:\Users\tukuyoma\Desktop\odemru\server\node_modules\body-parser\lib\read.js:130:5
    at invokeCallback (C:\Users\tukuyoma\Desktop\odemru\server\node_modules\raw-body\index.js:224:16)
[nodemon] restarting due to changes...
[nodemon] starting `babel-node ./src/server.js`


在http_adapter.js中,更改

controller(httpRequest)
        .then((httpResponse) => {
            if (httpResponse.headers) {
                res.set(httpResponse.headers);
            }
            res.type('json');
            res.status(httpResponse.statusCode).send(httpResponse.body);
        })
        .catch((e) =>
            res.status(500).send({ error: 'An unkown error occurred.' })
        );

try{
    const httpResponse =  controller(httpRequest);
    if (httpResponse.headers) {
       res.set(httpResponse.headers);
    }
    res.type('json');
    res.status(httpResponse.statusCode).send(httpResponse.body);
} 
catch (err) {
    res.status(500).send({ error: 'An unkown error occurred.' });
}

如果其他一切正常,应该可以工作。