【发布时间】:2021-03-24 21:32:00
【问题描述】:
我的 express js 路由给了我错误 500 内部服务器错误,我尝试控制台记录变量,但没有任何显示
这里是快速路线:
submitStar() {
this.app.post("/submitstar", async (req, res) => {
if(req.body.address && req.body.message && req.body.signature && req.body.star) {
const address = req.body.address;
const message = req.body.message;
const signature = req.body.signature;
const star = req.body.star;
try {
let block = await this.blockchain.submitStar(address, message, signature, star);
if(block){
return res.status(200).json(block);
} else {
return res.status(500).send("An error happened!");
}
} catch (error) {
return res.status(500).send(error);
}
} else {
return res.status(500).send("Check the Body Parameter!");
}
});
}
我不断收到消息“检查身体参数!”在邮递员中,而消息实际上是正确的
app.js:
const express = require("express");
const morgan = require("morgan");
const bodyParser = require("body-parser");
/**
* Require the Blockchain class. This allow us to have only one instance of the class.
*/
const BlockChain = require('./src/blockchain.js');
class ApplicationServer {
constructor() {
//Express application object
this.app = express();
//Blockchain class object
this.blockchain = new BlockChain.Blockchain();
//Method that initialized the express framework.
this.initExpress();
//Method that initialized middleware modules
this.initExpressMiddleWare();
//Method that initialized the controllers where you defined the endpoints
this.initControllers();
//Method that run the express application.
this.start();
}
initExpress() {
this.app.set("port", 8000);
}
initExpressMiddleWare() {
this.app.use(morgan("dev"));
this.app.use(bodyParser.urlencoded({extended:true}));
this.app.use(bodyParser.json());
}
initControllers() {
require("./BlockchainController.js")(this.app, this.blockchain);
}
start() {
let self = this;
this.app.listen(this.app.get("port"), () => {
console.log(`Server Listening for port: ${self.app.get("port")}`);
});
}
}
new ApplicationServer();
服务器有什么问题?
【问题讨论】:
-
req.body 是否包含您提到的键?如果不是,您可能必须使用正文解析器转换为 json 对象。
-
我尝试 console.log(req.body) 但终端没有显示任何内容
-
请检查 app.js 文件中的 body parser 包
-
用上面的路由器变量替换。它可能像 const router = express.Router();参考这个链接github.com/sujithmp/nodejs-auth-system-back-end-1/blob/master/…
标签: javascript node.js express blockchain