/
server.js
47 lines (37 loc) · 1.21 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Node.js Server.
Purpose:
- testing for multipart/form-data
*/
const http = require('http')
// Dispatching.
const server = http.createServer((req, res) => {
console.log(req.method, req.url)
if (req.url === '/') {
res.writeHeader(200, { 'content-type': 'text/html' })
res.end(`
<form method="POST" action="/upload" enctype="multipart/form-data">
<input type="text" name="message" value="Hello"/><br>
<input type="file" name="file"/><br>
<input type="submit" value="SUBMIT"/>
</form>
`)
} else if (req.url === '/upload' && req.method === 'POST') {
console.log('content-type header=====\n', req.headers['content-type'])
let body = []
req.on('data', chunk => {
body.push(chunk)
}).on('end', () => {
body = Buffer.concat(body).toString()
// console.log('body======\n' + body)
res.writeHeader(200, { 'content-type': 'text/html' })
res.end('thank you')
})
} else {
res.writeHeader(404)
res.end('NOT FOUND')
}
})
// Starts.
server.listen(8080)
console.log('Server is runnninng on :8080')