Bu bölümde, iki değeri HTML FORM POST metodu kullanarak istemci tarafından sunucu tarafına aktarmak için basit bir örnek anlatılacaktır. Girdiyi yönetmek için server.js içinde process_post yönlendirmede kullanılacaktır.
<html>
<body>
<form action = "http://127.0.0.1:8081/process_post" method = "POST">
First Name: <input type = "text" name = "first_name"> <br>
Last Name: <input type = "text" name = "last_name">
<input type = "submit" value = "Submit">
</form>
</body>
</html>
Yukarıdaki dosyanın ismi index2.htm olsun. Gelen GET isteği process_post üzerine (form action özelliğine bakın) yönlendireceğiz.
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
// POST islemini cozumlemek icin gerekli bir ayristirici
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.use(express.static('public'));
app.get('/index2.htm', function (req, res) {
res.sendFile( __dirname + "/" + "index2.htm" );
})
app.post('/process_post', urlencodedParser, function (req, res) {
// JSON formatinda hazirla
response = {
first_name:req.body.first_name,
last_name:req.body.last_name
};
console.log(response);
res.end(JSON.stringify(response));
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("Dinleniyor. http://%s:%s", host, port)
})
POST işlemi sonucu sunucu tarafına gelen verilerin ayrıştırılabilmesi için “body-parser” modülü kullanılmıştır.