Node.JS - MySQL API werkt niet

Stel hier je vragen met betrekking tot PHP, MySQL, HTML, CSS en JavaScript.
Forumregels
Check eerst onze helpsectie (https://www.antagonist.nl/help) voordat je hier een vraag stelt. Voor de meeste vragen hebben we uitgebreide handleidingen met uitleg.
TomasS
Berichten: 1
Lid geworden op: 05 aug 2021, 19:43

Node.JS - MySQL API werkt niet

Bericht door TomasS » 05 aug 2021, 19:57

Ik wil een eenvoudige API maken die gegevens uit een MySQL database weergeeft, maar ik krijg het niet aan de praat. MYSQL database met users tabel is gemaakt en bevat wat voorbeeld data, die ik zou moeten zien als ik de API aanroep, maar ik krijg een "Cannot GET /test1" foutmelding. Wat doe ik fout?

Mijn App.js:

Code: Selecteer alles

const express = require('express');
const mysql = require('mysql');

const app = express();

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

connection.connect((err) => {
  if (err) {
    console.log('error connecting: ' + err.stack);
    return;
  }
  console.log('success');
});

app.get('/', (req, res) => {
  connection.query(
    'SELECT * FROM users',
    (error, results) => {
      console.log(results);
      res.end(results);
    }
  );
});

app.listen();
En mijn Package.json:

Code: Selecteer alles

{
  "name": "nodejs_restapi",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "mysql": "^2.18.1"
  }
}
Ik zie wel wat foutmeldingen in de passanger.log:

Code: Selecteer alles

App 2108547 output: Warning: unable to write to /var/passenger/passenger.spawn.XXXX4dldd9/response/steps/subprocess_exec_wrapper/end_time: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received type number (1628193283.683)
App 2108547 output: Warning: unable to write to /var/passenger/passenger.spawn.XXXX4dldd9/response/steps/subprocess_wrapper_preparation/begin_time: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received type number (1628193283.688)
App 2108547 output: Warning: unable to write to /var/passenger/passenger.spawn.XXXX4dldd9/response/steps/subprocess_wrapper_preparation/begin_time: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received type number (1628193283.725)
App 2108547 output: Warning: unable to write to /var/passenger/passenger.spawn.XXXX4dldd9/response/steps/subprocess_wrapper_preparation/end_time: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received type number (1628193283.725)
App 2108547 output: Warning: unable to write to /var/passenger/passenger.spawn.XXXX4dldd9/response/steps/subprocess_app_load_or_exec/begin_time: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received type number (1628193283.726)
App 2108547 output: Warning: unable to write to /var/passenger/passenger.spawn.XXXX4dldd9/response/steps/subprocess_app_load_or_exec/begin_time: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received type number (1628193284.543)
App 2108547 output: Warning: unable to write to /var/passenger/passenger.spawn.XXXX4dldd9/response/steps/subprocess_app_load_or_exec/end_time: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received type number (1628193284.544)
App 2108547 output: Warning: unable to write to /var/passenger/passenger.spawn.XXXX4dldd9/response/steps/subprocess_listen/begin_time: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received type number (1628193284.544)
App 2108547 output: (node:2108547) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
App 2108547 output: (Use `node --trace-warnings ...` to show where the warning was created)
App 2108547 output: Warning: unable to write to /var/passenger/passenger.spawn.XXXX4dldd9/response/steps/subprocess_listen/begin_time: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received type number (1628193284.551)
App 2108547 output: Warning: unable to write to /var/passenger/passenger.spawn.XXXX4dldd9/response/steps/subprocess_listen/end_time: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received type number (1628193284.551)
App 2108547 output: success

Plaats reactie