使用带有 multipart/form-data 的 restify 发送文件导致超时问题
Sending file using restify with multipart/form-data causes a timeout problem
我有一个问题,因为我正在尝试在我的 NodeJS 服务器上使用 multipart / form-data 实现文件上传。当我调用上传时,我上传的文件出现在临时服务器文件夹中,但我的请求没有继续,我的客户端正在等待响应(在这种情况下,uploadFile 方法永远不会 运行)。
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();
}
}
我知道这是 8 个月后的事了,但你好像忘了在 uploadFile
中调用 next()
我有一个问题,因为我正在尝试在我的 NodeJS 服务器上使用 multipart / form-data 实现文件上传。当我调用上传时,我上传的文件出现在临时服务器文件夹中,但我的请求没有继续,我的客户端正在等待响应(在这种情况下,uploadFile 方法永远不会 运行)。
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();
}
}
我知道这是 8 个月后的事了,但你好像忘了在 uploadFile
next()