简体   繁体   English

在带有文件的Node.js服务POST中创建HTTP服务器

[英]Creating a HTTP Server in Node.js service POST with file

I would like to order sent to the POST request with a parameter in the form of a JPG file. 我想订购以JPG文件形式的参数发送给POST请求。

I use HttpClient in version 4.4.1 我在版本4.4.1中使用HttpClient

Part of the Java code looks like this: Java代码的一部分如下所示:

    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {
        File file = new File("path_to_jpg");
        HttpPost post = new HttpPost("http://localhost:1337/uploadJPG");
        FileBody fileBody = new FileBody(file);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addPart("upfile", fileBody);
        HttpEntity entity = builder.build();

        post.setEntity(entity);
        HttpResponse response = httpclient.execute(post);
        System.out.println(response.getEntity().getContent());

    } finally {
        httpclient.close();
    }

next at " http://localhost:1337/uploadJPG " want to let nodeJS have a server that will process the JPG file 接下来的“ http:// localhost:1337 / uploadJPG ”要让nodeJS有一个服务器来处理JPG文件

the idea of server code nodeJS: 服务器代码nodeJS的想法:

var http = require('http'),
fs = require('fs'),
server = http.createServer( function(req, res) {

    if (req.method == 'POST') {

        //process file JPG

        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end('processed JPG');
    }
});

port = 1337;
host = '127.0.0.1';
server.listen(1337, '127.0.0.1');
console.log('Listening at http://' + '127.0.0.1' + ':' + 1337);

and now my question is, How can I create such a service in NodeJS, which will have the file in jpg? 现在我的问题是,如何在NodeJS中创建这样的服务,它将在jpg中包含文件?

不确定是否使用Express ,但是可以使用像Multer这样的中间件,这使得处理MultiPart数据和文件非常简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM