Html 使用节点JS向XDB提交表单
我试图在NodeJS中创建一个web应用程序,以可视化一些图形,并从表单和按钮接收用户反馈,然后将它们写入一个inflow数据库。但是,我无法理解如何处理post数据并将其正确写入dbHtml 使用节点JS向XDB提交表单,html,node.js,influxdb,Html,Node.js,Influxdb,我试图在NodeJS中创建一个web应用程序,以可视化一些图形,并从表单和按钮接收用户反馈,然后将它们写入一个inflow数据库。但是,我无法理解如何处理post数据并将其正确写入db const Influx = require("influx"); const express = require("express"); const http = require("http"); const os = require("os
const Influx = require("influx");
const express = require("express");
const http = require("http");
const os = require("os");
let fs = require('fs');
const app = express();
const influx = new Influx.InfluxDB({
host: "localhost",
database: "trial",
schema: [
{
measurement: "form",
fields: {
name: Influx.FieldType.STRING,
surname: Influx.FieldType.STRING
},
tags: ["host"],
},
],
});
influx
.getDatabaseNames()
.then((names) => {
if (!names.includes("trial")) {
return influx.createDatabase("trial");
}
})
.then(() => {
http.createServer(app).listen(8000, function () {
console.log("Listening on port 8000");
})
.catch((err) => {
console.error(`Error creating Influx database!`);
});
});
app.use((req, res, next) => {
res.writeHead(200, {
'Content-Type': 'text/html'
});
fs.readFile('grafana.html', null, function (error, data) {
if (error) {
res.writeHead(404);
res.write('Whoops! File not found!');
} else {
res.write(data);
}
res.end();
});
});
到目前为止一切正常,我能够基于HTML代码可视化网页
<form action="/info" method="post">
First name:<br>
<input type="text" name="name"<br>
Last name:<br>
<input type="text" name="surname"><br>
<input type="submit" value="Submit">
</form>
我无法将表单数据写入数据库-数据库仍然为空,控制台不输出任何内容。我知道可能会有多个错误,因为我是web开发领域的完全初学者。我希望任何人都能帮忙
app.get('/info', function (req, res) {
res.send('info')
});
app.post('/', function (req, res) {
var form = {
name: req.body.name,
surname: req.body.surname
};
influx
.writePoints([
{
measurement: "form",
tags: { host: os.hostname() },
fields: { form, path: req.path },
},
])
.catch((err) => {
console.error(`Error saving data to InfluxDB! ${err.stack}`);
});
console.log(form);
});