我正在为自己尝试开发的项目设置服务器,并分别使用SQLite和Node.js作为数据库类型和服务器平台。我对后端开发相对较新,因此我尝试设置从网页前端到后端的POST请求,以便用户提交的任何内容都将存储在“ comments.db”中,该文件将存储在服务器中所有用户名和数据(特别是用户ID和数据)。
过去,我曾尝试过以不同的方式重命名数据库,并将数据库移到几个不同的子文件中,但是在提交时总是会产生相同的错误。
以下是我的node.js文件代码:
var os = require( 'os' );
const interfaces = os.networkInterfaces();
const addresses = [];
var getMacAddress;
var request = require('request');
var express = require('express');
var sqlite3 = require('sqlite3');
var bodyParser = require('body-parser');
var ip = require("ip");
var address = ip.address();
var db = new sqlite3.Database('comments.db');
var app = express();
app.use(express.static(__dirname));
app.use(bodyParser.urlencoded({extended: false}));
app.get('/comments', function(request, response){
// response.send('Hello Worlds');
response.send("Hello my name is aestheticnoodle and you have reached a wrong");
});
app.get('/comments', function(request, response){
console.log('GET request received at /alpha');
db.all('SELECT * FROM comments', function(err, rows){
if(err){
console.log("Error");
} else {
response.send(rows);
//pass rows back to the client
}
});
});
//runs once a user submits a comment
app.post('/comments', function(request, response){
db.run('INSERT INTO comments VALUES (?, ?)', ["aestheticnoodle" ,request.body.action], function(err, rows){
if(err){
console.log(request.body.action);
console.log("INSERT INTO " + err);
} else {
response.status(200).redirect('chat.html');
}
});
});
app.listen(3000, function(){
console.log("Server is running on port 3000");
});
var admin = require("firebase-admin");
var firebaseConfig = {
apiKey: "baseapp",
authDomain: "fire",
databaseURL: "inthe,
projectId: "data",
storageBucket: "",
messagingSenderId: "private",
appId: "some2"
};
admin.initializeApp(firebaseConfig);
var headers = {
'App-Id': 'someappid',
'App-Key': 'someappkey',
'Content-Type': 'application/json'
};
var dataString = '{"text": "test run.."}';
var options = {
url: 'https://somesite.com/v2/parse',
method: 'POST',
headers: headers,
body: dataString
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);
,错误如下:
C:\Users\achu1\Music\projectsummer2019\cybeRx>node nodeRequest.js
Server is running on port 3000
undefined
INSERT INTO Error: SQLITE_ERROR: no such table: comments
这是我尝试在聊天机器人界面中插入html的javascript代码段:
var string = "What seems to be the problem you have been experiencing? Use one of the keywords, per <a href = \"symptomlist.html\" target=\"_blank\"> SymptomList</a> the complete list of symptoms."
document.getElementById("chat").innerHTML += '<form action = "/comments" method = "POST">'+ "<div id = \"botLog\" class = \"chatting\">" + string + '<textarea rows = "15" name = "comment"> Write stuff here' + '</textarea>'+ '<br><br><input type = "submit" value = "SUBMIT"/></form>' + "</div><br>";
如您所见,我试图通过将localhost
方法形式制作到POST
文件中来连接数据库和运行在comment.db
中的html文件,该文件实际上位于我的{目录。
为什么节点说没有这样的数据库来执行POST
命令,以及如何解决此问题,以便我的文本框数据保存到数据库中?