Home  >  Article  >  Web Front-end  >  nodejs database to json

nodejs database to json

WBOY
WBOYOriginal
2023-05-28 09:48:07806browse

Node.js is a JavaScript environment that runs on the server side. It allows JavaScript to run on the server side and is widely used in web development. Node.js has the advantages of asynchronous non-blocking I/O processing capabilities, modular development, and rich third-party modules, making it a very popular technology in server-side development.

In Node.js development, database operations are often involved, and converting data in the database into JSON format is a common requirement. This article will introduce how to convert data in the database into JSON format.

1. Connect to the database

In Node.js, you can use third-party modules to connect to the database. Here, mysql is used as an example for explanation. First, you need to install the mysql module in the project, enter the following command in the terminal or command line:

npm install mysql --save

After the installation is completed, you can introduce the mysql module into the code:

const mysql = require('mysql');

Next, you need to configure Database connection information:

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'database_name'
});

Among them, the configuration items include:

  • host: database host name, here is the local host;
  • user: database user name;
  • password: database password;
  • database: name of the database to be connected.

2. Query data

After successfully connecting to the database, you can query the data. In Node.js, you can use the query method provided by the mysql module to query data. The following is a sample code for querying data:

connection.query('SELECT * FROM table_name', function (error, results, fields) {
  if (error) throw error;
  console.log(results);
});

The first parameter is the SQL statement to be executed, and the second parameter is the callback function, which will be executed when the query is completed. The query results are returned in the form of an array, and each element is an object of one row of data.

3. Convert to JSON format

After the query is completed, the obtained data can be converted to JSON format for convenient use and transmission. In Node.js, you can use the JSON.stringify method to convert data into JSON format. The following is a sample code converted to JSON format:

connection.query('SELECT * FROM table_name', function (error, results, fields) {
  if (error) throw error;
  const json_data = JSON.stringify(results);
  console.log(json_data);
});

Among them, json_data saves the converted JSON string.

The complete code is as follows:

const mysql = require('mysql');
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'database_name'
});

connection.connect();

connection.query('SELECT * FROM table_name', function (error, results, fields) {
  if (error) throw error;
  const json_data = JSON.stringify(results);
  console.log(json_data);
});

connection.end();

The above is the complete process of converting the data in the database into JSON format. By using Node.js and mysql modules, the database can be easily operated.

The above is the detailed content of nodejs database to json. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn