简体   繁体   中英

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.

I use HttpClient in version 4.4.1

Part of the Java code looks like this:

    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

the idea of server code 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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