这段代码是我写的
var http = require('http');
var express = require('express');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: '',
database: 'nodejs'
});
connection.connect(function (err) {
if (!err) {
connection.query('SELECT * FROM nodetest', function (err, rows) {
if (err) {
console.log(err);
return;
}
var json = JSON.parse(JSON.stringify(rows))[0];
console.log(json);
});
console.log("Database is connected");
} else {
console.log("Error connecting database");
}
});
http.createServer(function (request, response) {
response.writeHead(200, {
"Content-Type": "test/html"
});
response.end('here my data');
}).listen(7777);
我在本地主机中以 json 形式显示结果时遇到问题
读取表格并将其转换为 json 并在本地主机中显示的最简单方法是什么
最佳答案
JSON.stringify 是什么意思?做什么?
The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified.
JSON.parse 是什么意思?做什么?
The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.
所以基本上,一个操作是另一个操作的逆操作。这就是你的问题:
var json = JSON.parse(JSON.stringify(rows))[0];
关于javascript - nodeJS - 我如何连接 MySQL 并在本地主机中显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43912388/