javascript - express js内部服务器错误500,代码中没有错误
问题描述
我的快速 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();
服务器可能有什么问题?
解决方案
对代码做了一些改动。
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.router = express.Router();
this.router.use(bodyParser.urlencoded({extended:true}));
this.router.use(bodyParser.json());
}
initControllers() {
require("./BlockchainController.js")(this.app, this.blockchain);
// console.log(this.app.route(),"this.app")
}
start() {
let self = this;
this.router.post('/',(req,res) => {
console.log(req.body.id,"body");
res.status(200).send('Hello');
})
this.app.use(this.router);
this.app.listen(this.app.get("port"), (req,res) => {
console.log(`Server Listening for port: ${self.app.get("port")}`);
});
}
}
new ApplicationServer();
推荐阅读
- printing - 带有 Zebra ZM600 的 Ghostscript
- debugging - Firefox 调试,从智能手机上的台式电脑打开 localhost
- python - 在 TKinter GUI 中使用线程
- python - 将三个列表与概率进行比较并定义 argmax 规则
- java - 当列表分页的大小输入设置为大于 15 时,Spring Data PagedListHolder 没有返回正确的 PageSize,为什么?
- swagger - NestJS 映射类型 - 嵌套对象
- javascript - Firefox 的 Imacros - 是否可以在 javascript 中使用 match 但从数据源读取而不是直接在宏中声明?
- javascript - 当我进入下一个问题时,为什么最后选中的复选框保留它?反应式
- javascript - 如何在按钮单击中设置 Vaadin 8 LoadingIndicatorConfiguration
- wso2 - 验证和授权 REST API