使用restify和multipart / form-data发送文件会导致超时问题

问题描述 投票:1回答:1

我有一个问题,因为我试图在NodeJS服务器上使用multipart / form-data实现文件上传。当我调用上载时,我上载的文件会出现在临时服务器文件夹中,但我的请求不会继续,并且我的客户端正在等待响应(在这种情况下,上载File方法永远不会运行)。

enter image description here

upload.router.ts

import {Router} from '../common/router';
import * as restify from 'restify';

class UploadRouter extends Router {

    uploadFile = (req, resp, next) => {
        console.log(req);
        resp.json('test');
    };

    applyRoutes(application: restify.Server) {
        this.basePath = '/upload';

        application.post(`${this.basePath}`, this.uploadFile);
    }
}

export const uploadRouter = new UploadRouter();

server.ts

export class Server {
    application: restify.Server;

    initRoutes(routers: Router[]): Promise<any> {
        return new Promise((resolve, reject) => {
            try {
                const options: restify.ServerOptions = {
                    name: environment.project.name,
                    version: environment.project.version
                };

                if (environment.security.enableHTTPS) {
                    options.certificate = fs.readFileSync(environment.security.certificate);
                    options.key = fs.readFileSync(environment.security.key);
                }

                this.application = restify.createServer(options);
                this.connector = blockchainConnector(environment.blockchain.connector);

                const corsOptions: corsMiddleware.Options = {
                    preflightMaxAge: 10,
                    origins: ['*'],
                    allowHeaders: ['*'],
                    exposeHeaders: []
                };
                const cors: corsMiddleware.CorsMiddleware = corsMiddleware(corsOptions);

                this.application.pre(cors.preflight);

                this.application.use(cors.actual);
                this.application.use(restify.plugins.queryParser());
                this.application.use(restify.plugins.bodyParser());
                this.application.use(restify.plugins.acceptParser(this.application.acceptable));
                this.application.use(restify.plugins.fullResponse());
                this.application.use(restify.plugins.multipartBodyParser({
                    multiples: true,
                    mapParams: true,
                    mapFiles: true,
                    keepExtensions: true,
                    uploadDir: environment.directory.tempDir
                }));
                this.application.use(mergePatchBodyParser);
                this.application.use(tokenParser);

                // routes
                for (let router of routers) {
                    router.applyRoutes(this.application, this.connector);
                    indexRouter.addRouter(router);
                }
                indexRouter.applyRoutes(this.application);

                this.application.listen(environment.server.port, () => {
                    resolve(this.application);
                });

                this.application.on('restifyError', handleError);

            } catch (error) {
                reject(error);
            }
        })
    }

    bootstrap(routers: Router[] = []): Promise<Server> {
        return this.initRoutes(routers).then(() => this);
    }

    shutdown() {
        this.application.close();
    }
}
node.js multipartform-data restify
1个回答
0
投票

我知道这是8个月后,但是您似乎忘记了在next()中呼叫uploadFile

© www.soinside.com 2019 - 2024. All rights reserved.