Home >Web Front-end >Front-end Q&A >How to get nodejs data

How to get nodejs data

PHPz
PHPzOriginal
2023-05-18 11:22:07901browse

Node.js is a popular JavaScript runtime environment commonly used for server-side programming. When developing Node.js applications, it is often necessary to obtain data from different data sources, such as databases, API interfaces, etc. This article explains how to obtain data from different data sources.

  1. Get data from a file

In Node.js, you can use the fs module to read files. The following is a simple example:

const fs = require('fs');
fs.readFile('data.txt', 'utf8', function(err, data) {
  if (err) throw err;
  console.log(data);
});

The above code reads a text file named data.txt and prints its contents to the console.

  1. Get data from the database

Node.js supports a variety of databases, such as MySQL, MongoDB, PostgreSQL, etc. The corresponding driver can be used to connect and query the database. The following is an example using the MySQL driver:

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

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

The above code connects to the MySQL database named my_database and queries all records in the my_table table. The query results will be printed to the console.

  1. Get data from API interface

API interface is a general data acquisition method, often used to obtain data provided by Web services. Fetching data from API interfaces is easy with Node.js. You can use Node.js's built-in http module or use a third-party library such as axios to initiate http requests. The following is an example of using the axios library:

const axios = require('axios');
axios.get('https://api.example.com/data')
  .then(function (response) {
    console.log(response.data);
  })
  .catch(function (error) {
    console.log(error);
  });

The above code uses the axios.get method to obtain the data returned by the API interface named data. The request results will be printed to the console.

Summary

There are different ways to get data in Node.js, and you can use different methods to get data from different data sources. The above describes how to obtain data from files, databases and API interfaces. If you need to obtain data according to actual needs, you can learn more in-depth and understand according to relevant documents and tutorials.

The above is the detailed content of How to get nodejs data. 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
Previous article:vue changes border colorNext article:vue changes border color