Chapter 4,6: prettier updates and web framework samples
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<form method="POST" enctype="multipart/form-data">
|
||||
<label for="userfile">File:</label>
|
||||
<input type="file" id="userfile" name="userfile"><br>
|
||||
<input type="submit">
|
||||
<label for="userfile">File:</label>
|
||||
<input type="file" id="userfile" name="userfile" /><br />
|
||||
<input type="submit" />
|
||||
</form>
|
||||
|
||||
@@ -1,55 +1,58 @@
|
||||
const fs = require('fs')
|
||||
const http = require('http')
|
||||
const path = require('path')
|
||||
const fs = require("fs");
|
||||
const http = require("http");
|
||||
const path = require("path");
|
||||
|
||||
const form = fs.readFileSync(path.join(__dirname, 'public', 'form.html'))
|
||||
const form = fs.readFileSync(path.join(__dirname, "public", "form.html"));
|
||||
|
||||
const formidable = require('formidable')
|
||||
const formidable = require("formidable");
|
||||
|
||||
|
||||
http.createServer((req, res) => {
|
||||
if (req.method === 'GET') {
|
||||
get(res)
|
||||
return
|
||||
http
|
||||
.createServer((req, res) => {
|
||||
if (req.method === "GET") {
|
||||
get(res);
|
||||
return;
|
||||
}
|
||||
if (req.method === 'POST') {
|
||||
post(req, res)
|
||||
return
|
||||
if (req.method === "POST") {
|
||||
post(req, res);
|
||||
return;
|
||||
}
|
||||
error(405, res)
|
||||
}).listen(3000)
|
||||
error(405, res);
|
||||
})
|
||||
.listen(3000);
|
||||
|
||||
function get(res) {
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/html'
|
||||
})
|
||||
res.end(form)
|
||||
res.writeHead(200, {
|
||||
"Content-Type": "text/html",
|
||||
});
|
||||
res.end(form);
|
||||
}
|
||||
|
||||
function post(req, res) {
|
||||
if (!/multipart\/form-data/.test(req.headers['content-type'])) {
|
||||
error(415, res)
|
||||
return
|
||||
}
|
||||
if (!/multipart\/form-data/.test(req.headers["content-type"])) {
|
||||
error(415, res);
|
||||
return;
|
||||
}
|
||||
|
||||
const form = formidable({
|
||||
multiples: true,
|
||||
uploadDir: './uploads'
|
||||
const form = formidable({
|
||||
multiples: true,
|
||||
uploadDir: "./uploads",
|
||||
});
|
||||
|
||||
form.parse(req, (err, fields, files) => {
|
||||
if (err) return err;
|
||||
res.writeHead(200, {
|
||||
"Content-Type": "application/json",
|
||||
});
|
||||
|
||||
form.parse(req, (err, fields, files) => {
|
||||
if (err) return err
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
res.end(JSON.stringify({
|
||||
fields,
|
||||
files
|
||||
}))
|
||||
})
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
fields,
|
||||
files,
|
||||
})
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function error(code, res) {
|
||||
res.statusCode = code
|
||||
res.end(http.STATUS_CODES[code])
|
||||
}
|
||||
res.statusCode = code;
|
||||
res.end(http.STATUS_CODES[code]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user