未找到模块:Can';t解决';财政司司长';在'/节点模块/销毁';使用Next.js mysql Express
我想将pages/index.js中的数据插入mysql数据库。 在routes/routes.js中有一个mysql连接,我有内置函数来调用我想要的 结构未找到模块:Can';t解决';财政司司长';在'/节点模块/销毁';使用Next.js mysql Express,mysql,reactjs,express,next.js,Mysql,Reactjs,Express,Next.js,我想将pages/index.js中的数据插入mysql数据库。 在routes/routes.js中有一个mysql连接,我有内置函数来调用我想要的 结构 组成部分 页数 index.js 路线 routes.js server.js package.json 失败并出现错误: 未找到模块:无法解析“…/node\u modules/destroy”中的“fs” pages/index.js fetch("http://localhost:3001/api/secret", {
- 组成部分
- 页数
- index.js
- 路线
- routes.js
- server.js
- package.json
pages/index.js
fetch("http://localhost:3001/api/secret", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
cid: dataJson.ID,
HOSPCODE: HOSPCODE,
spk: dataJson.SPK,
secretkey: dataJson.SecretKey
})
});
import React,{Component}来自“React”;
从“@material ui/core”导入{排版、按钮、网格};
从“QRCode.react”导入QRCode;
从“下一个/动态”导入动态;
从“./stellar”导入{PublicKey、SecretKey、HOSPCODE};
const{ins}=require(“../routes/routes”)//这就是问题所在!
const StellarSdk=require(“stellar sdk”);
常量QrReader=动态(()=>导入(“反应qr读取器”){
ssr:错误
});
导出默认类组件{
状态={
结果:“,
摄影机:是的,
味精:“,
QR:{}
};
_清除=()=>{
this.setState({result:,camera:true,msg:,QR:{});
};
handleScan=数据=>{
如果(数据){
const dataJson=JSON.parse(数据);
如果(dataJson.Type==“患者”){
const KP=StellarSdk.Keypair.fromSecret(SecretKey);
这是我的国家({
结果:dataJson,
QR:JSON.stringify({
类型:“医院”,
医院名称:“xxx医院”,
终点:“xxx/patientID_uu”,
公开密钥,
签名:KP.sign(
from(dataJson.ID+dataJson.SPK)
).toString(“base64”)
}),
摄像机:错,
味精:“
});
ins(dataJson.ID、HOSPCODE、dataJson.SPK、dataJson.SecretKey);
}否则{
这是我的国家({
消息:“错误的QRCode。”
});
}
}
};
但是/routes/routes
在server.js
中工作
const express=require(“express”);
const next=要求(“下一步”);
const dev=process.env.NODE_env!=“生产”;
const app=next({dev});
const handle=app.getRequestHandler();
const bodyParser=require(“body parser”);
const{router,ins}=require(“./路由/路由”);
应用程序
.准备
.然后(()=>{
const server=express();
use(bodyParser.json());
use(bodyParser.urlencoded({extended:false}));
使用(“/api”,路由器);
server.get(“*”,(req,res)=>{
返回句柄(req、res);
});
监听(3001,错误=>{
如果(错误)抛出错误;
console.log(“>已准备就绪http://localhost:3001");
});
})
.catch(ex=>{
控制台错误(例如堆栈);
过程。退出(1);
});
routes/routes.js
fetch("http://localhost:3001/api/secret", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
cid: dataJson.ID,
HOSPCODE: HOSPCODE,
spk: dataJson.SPK,
secretkey: dataJson.SecretKey
})
});
const express=require(“express”);
const mysql=require(“mysql”);
const router=express.router();
const dotenv=require(“dotenv”).config();
const connection=mysql.createConnection({
主机:process.env.DATABASE\u主机,
数据库:process.env.database_NAME,
用户:process.env.DATABASE_用户名,
密码:process.env.DATABASE\u密码,
港口:3306
});
console.log(“连接…”);
connection.connect(函数(err){
if(err)返回新错误(“SQL连接期间发生错误”+err);
console.log(“已连接!”);
});
log(connection.config);
/*获取主页*/
router.get(“/”,函数(req,res,next){
...
...
...
ins=(cid,HOSPCODE,spk,secretKey)=>(cid,HOSPCODE,spk,secretKey)=>{
var sql=“插入到STELLARKEY(CID、HOSPCODE、SPK、SecretKey)值中?”;
var值=[[cid、HOSPCODE、spk、secretkey]];
查询(sql,[values],函数(err,result){
如果(错误)抛出错误;
});
};
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版本14.14发行版5.7.28,
节点v12.14.1最后,我必须通过前端的同一台服务器(index.js)向我的服务器发送请求,将数据插入mysql数据库。 我不确定。由于SSR(Next.js),我无法将数据发送回后端 但是 我仍然需要其他方法,除了将请求发布到自身中
routes/routes.js
fetch("http://localhost:3001/api/secret", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
cid: dataJson.ID,
HOSPCODE: HOSPCODE,
spk: dataJson.SPK,
secretkey: dataJson.SecretKey
})
});
router.post(“/secret/”),cors(corsOptions),函数(req,res,next){
var sql=“插入到STELLARKEY(CID、HOSPCODE、SPK、SecretKey)值中?”;
var值=[
[req.body.cid,req.body.HOSPCODE,req.body.spk,req.body.secretkey]
];
查询(sql,[values],函数(err,result){
if(err)console.log(err);
});
});
module.exports=路由器;
pages/index.js
fetch("http://localhost:3001/api/secret", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
cid: dataJson.ID,
HOSPCODE: HOSPCODE,
spk: dataJson.SPK,
secretkey: dataJson.SecretKey
})
});
server.js
fetch("http://localhost:3001/api/secret", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
cid: dataJson.ID,
HOSPCODE: HOSPCODE,
spk: dataJson.SPK,
secretkey: dataJson.SecretKey
})
});
const router=require(“./路由/路由”);
应用程序
.准备
.然后(()=>{
const server=express();
use(bodyParser.json());
use(bodyParser.urlencoded({extended:false}));
使用(“/api”,路由器);
server.get(“*”,(req,res)=>{
返回句柄(req、res);
});
只要我们不运行下一个构建,也不启动带有节点的应用程序,这个问题就不会发生。
我们现在使用NODE_ENV=dev来规避这个问题。我的意思是我们现在使用npm run dev来部署。这只能被认为是一个临时解决方案
"scripts": {
"dev": "NODE_ENV=dev node server.js",
"start": "node server.js",
"deploy": "next build && NODE_ENV=production node server.js"
}
请添加文本而不是图像