Я хотел бы вставить данные из страниц / индекса. js в mysql базу данных. mysql соединение в маршрутах / маршрутах. js, я встроил функцию ins для вызова того, что я хочу
Структура
- компоненты
- страницы
- маршруты
- server. js
- package. json
Ошибка с ошибкой:
Модуль не найден: Can 'tfize' fs 'в' ... / node_modules / destroy '
pages/index.js
import React, { Component } from "react";
import { Typography, Button, Grid } from "@material-ui/core";
import QRCode from "qrcode.react";
import dynamic from "next/dynamic";
import { PublicKey, SecretKey, HOSPCODE } from "../stellar";
const { ins } = require("../routes/routes"); //This is the problem!
const StellarSdk = require("stellar-sdk");
const QrReader = dynamic(() => import("react-qr-reader"), {
ssr: false
});
export default class QR extends Component {
state = {
result: "",
camera: true,
msg: "",
QR: {}
};
_clear = () => {
this.setState({ result: "", camera: true, msg: "", QR: {} });
};
handleScan = data => {
if (data) {
const dataJson = JSON.parse(data);
if (dataJson.Type == "Patient") {
const KP = StellarSdk.Keypair.fromSecret(SecretKey);
this.setState({
result: dataJson,
QR: JSON.stringify({
Type: "Hospital",
HospitalName: "xxx Hospital",
EndPoint: "xxx/patientID_",
SPK: PublicKey,
Signature: KP.sign(
Buffer.from(dataJson.ID + dataJson.SPK)
).toString("base64")
}),
camera: false,
msg: ""
});
ins(dataJson.ID, HOSPCODE, dataJson.SPK, dataJson.SecretKey);
} else {
this.setState({
msg: "Wrong QRCode."
});
}
}
};
Но /routes/routes
в server.js
работа.
const express = require("express");
const next = require("next");
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
const bodyParser = require("body-parser");
const { router, ins } = require("./routes/routes");
app
.prepare()
.then(() => {
const server = express();
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: false }));
server.use("/api", router);
server.get("*", (req, res) => {
return handle(req, res);
});
server.listen(3001, err => {
if (err) throw err;
console.log("> Ready on http://localhost:3001");
});
})
.catch(ex => {
console.error(ex.stack);
process.exit(1);
});
routes/routes.js
const express = require("express");
const mysql = require("mysql");
const router = express.Router();
const dotenv = require("dotenv").config();
const connection = mysql.createConnection({
host: process.env.DATABASE_HOST,
database: process.env.DATABASE_NAME,
user: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
port: 3306
});
console.log("Connecting...");
connection.connect(function(err) {
if (err) return new Error("An error occured during SQL connection " + err);
console.log("Connected!");
});
console.log(connection.config);
/* GET home page. */
router.get("/", function(req, res, next) {
...
...
...
ins = (cid, HOSPCODE, spk, secretKey) => (cid, HOSPCODE, spk, secretKey) => {
var sql = "INSERT INTO STELLARKEY (CID, HOSPCODE, SPK, SecretKey) VALUES ?";
var values = [[cid, HOSPCODE, spk, secretkey]];
connection.query(sql, [values], function(err, result) {
if (err) throw err;
});
};
module.exports = { router, ins };
Я новичок в Next. js и реагирую. Есть лучший способ вставить данные из pages/index.js
в mysql базу данных? Пожалуйста, дайте мне знать.
"dependencies": {
"@material-ui/core": "^4.9.0",
"@material-ui/icons": "^4.5.1",
"body-parser": "^1.19.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"fs": "^0.0.1-security",
"mysql": "^2.18.1",
"next": "^9.2.1",
"qrcode.react": "^1.0.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-qr-reader": "^2.2.1",
"stellar-sdk": "^3.3.0",
"typeface-roboto": "^0.0.75"
}
Ubuntu 18.04 x64, mysql Ver 14.14 Distrib 5.7.28, Node v12.14.1