如何解决使用Node JS将表单提交到InfluxDB
我正在尝试在节点JS中创建一个Web应用程序,以可视化一些图形并接收来自表单和按钮的用户反馈,然后将它们写入Influx数据库。但是,我无法理解如何处理发布数据并将其正确写入数据库。
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>
并在Influx上创建“试用”数据库。
这是行不通的:
app.get('/info',function (req,res) {
res.send('info')
});
app.post('/',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);
});
我无法将表单数据写入db-数据库保持为空,并且控制台不输出任何内容。我知道可能是多个错误,因为我是Web开发领域的一个完整的初学者。我希望任何人都能帮忙!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。