【问题标题】:express js internal server error 500 with no errors in codeexpress js内部服务器错误500,代码中没有错误
【发布时间】: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


【解决方案1】:

对代码做了一些改动。

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

【讨论】:

  • 它给我的“路由器”未定义
  • 我实现了它并没有帮助,而且还有其他路线在没有它的情况下也可以工作
  • 你的路线在哪里?您需要使用带有路由器的正文解析器
  • 实际上我认为服务器不需要正文解析器,因为没有它其他请求也可以正常工作
猜你喜欢
  • 2022-09-23
  • 1970-01-01
  • 2017-09-03
  • 1970-01-01
  • 1970-01-01
  • 2014-01-26
  • 1970-01-01
  • 1970-01-01
  • 2010-11-07
相关资源
最近更新 更多