Свойства объектов Json можно получить с помощью оператора точек.
Файл узла:
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/test',connectCallback);
function connectCallback(err, db) {
col = db.collection('persons');
console.log('Connected to mongodb');
}
var app = express();
//middlewares
app.use(bodyParser());
//routes
app.get('/', function (req, res) {
res.sendFile(__dirname+'/index.html');
});
app.post('/persons', (req, res) => {
req.body.ratio=0
col.save( req.body, (err, result) => {
if (err)
return console.log(err)
console.log(result)
console.log('saved to database')
console.log(req.body)
res.redirect('/')
})
})
setTimeout(function() {
app.listen(3000, function () {
console.log('Sample app listening on port 3000!');
});
}, 1000);
Файл index.html:
<html>
<body>
<form action="/persons" method="POST">
Name:<input type="text" name="name"><br>
URL:<input type="text" name="quote"><br>
<button type="submit">Submit</button>
</form>
</body>
</html>