0
express를 사용하지 않고 node.js 파일에 업로드하는 것이 가장 좋은 방법은 무엇입니까?Node.js : 브라우저 양식에서 업로드 한 파일을 저장하는 방법은 무엇입니까?
express를 사용하지 않고 node.js 파일에 업로드하는 것이 가장 좋은 방법은 무엇입니까?Node.js : 브라우저 양식에서 업로드 한 파일을 저장하는 방법은 무엇입니까?
Express는 nodejs 코어 모듈이 제공하는 원시 Http 모듈을 기반으로 구축됩니다. 스트림입니다 http.createServer
requestListener의 각 req
및 res
때문에 대한 문서를보세요, 당신은 실제로 fs
코어 모듈로 아주 쉽게이 작업을 수행 할 수 있습니다.
var http = require('http');
var fs = require('fs');
var server = http.createServer(function requestListener (req, res) {
req.once('end', function onEnd() {
res.statusCode = 200;
res.end('Uploaded File\n');
});
req.pipe(fs.createWriteStream('./uploadedFile.txt'));
});
server.listen(8080);
파일을 업로드하십시오 :
# file contains "hello world!"
curl -v -d @test.txt localhost:8080
* Rebuilt URL to: localhost:8080/
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> POST/HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.51.0
> Accept: */*
> Content-Length: 12
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 12 out of 12 bytes
< HTTP/1.1 200 OK
< Date: Fri, 21 Apr 2017 01:34:21 GMT
< Connection: keep-alive
< Content-Length: 14
<
Uploaded File
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact
을하고 파일
cat uploadedFile.txt
hello world!
희망이 도움이 체크 아웃!