首页 > 解决方案 > 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();

服务器可能有什么问题?

标签: javascriptnode.jsexpressblockchain

解决方案


对代码做了一些改动。

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();

推荐阅读