Chapter 4: initial recipe samples
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
const http = require('http')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
const form = fs.readFileSync(path.join(__dirname, 'public', 'form.html'))
|
||||
|
||||
http.createServer((req, res) => {
|
||||
if (req.method === 'GET') {
|
||||
get(res)
|
||||
return
|
||||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
post(req, res)
|
||||
return
|
||||
}
|
||||
error(405, res)
|
||||
}).listen(3000)
|
||||
|
||||
function get(res) {
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/html'
|
||||
})
|
||||
res.end(form)
|
||||
}
|
||||
|
||||
function post(req, res) {
|
||||
if (req.headers['content-type'] !== 'application/json') {
|
||||
error(415, res)
|
||||
return
|
||||
}
|
||||
|
||||
let input = '';
|
||||
|
||||
req.on('data', chunk => {
|
||||
input += chunk.toString()
|
||||
})
|
||||
|
||||
req.on('end', () => {
|
||||
const parsed = JSON.parse(input)
|
||||
|
||||
if (parsed.err) {
|
||||
error(400, 'Bad Request', res)
|
||||
return
|
||||
}
|
||||
|
||||
console.log('Received data: ', parsed)
|
||||
res.end('{"data": ' + input + "}")
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
function error(code, res) {
|
||||
res.statusCode = code
|
||||
res.end(http.STATUS_CODES[code])
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<form method="POST">
|
||||
<label for="forename">Forename:</label>
|
||||
<input id="forename" name="forename">
|
||||
<label for="surname">Surname:</label>
|
||||
<input id="surname" name="surname">
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
|
||||
<script>
|
||||
document.forms[0].addEventListener('submit', (event) => {
|
||||
event.preventDefault()
|
||||
|
||||
let data = {
|
||||
'forename': document.getElementById('forename').value,
|
||||
'surname': document.getElementById('surname').value
|
||||
};
|
||||
console.log('data', data);
|
||||
|
||||
fetch('http://localhost:3000', {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
}).then(function (response) {
|
||||
console.log(response);
|
||||
return response.json();
|
||||
});
|
||||
});
|
||||
</script>
|
||||
@@ -0,0 +1,48 @@
|
||||
const http = require('http')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
const form = fs.readFileSync(path.join(__dirname, 'public', 'form.html'))
|
||||
|
||||
http.createServer((req, res) => {
|
||||
if (req.method === 'GET') {
|
||||
get(res)
|
||||
return
|
||||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
post(req, res)
|
||||
return
|
||||
}
|
||||
error(405, res)
|
||||
}).listen(3000)
|
||||
|
||||
function get(res) {
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/html'
|
||||
})
|
||||
res.end(form)
|
||||
}
|
||||
|
||||
function post(req, res) {
|
||||
if (req.headers['content-type'] !== 'application/x-www-form-urlencoded') {
|
||||
error(415, res)
|
||||
return
|
||||
}
|
||||
|
||||
let input = '';
|
||||
|
||||
req.on('data', chunk => {
|
||||
input += chunk.toString()
|
||||
})
|
||||
|
||||
req.on('end', () => {
|
||||
console.log(input);
|
||||
res.end(http.STATUS_CODES[200])
|
||||
})
|
||||
}
|
||||
|
||||
function error(code, res) {
|
||||
res.statusCode = code
|
||||
res.end(http.STATUS_CODES[code])
|
||||
}
|
||||
Reference in New Issue
Block a user