【问题标题】:Didn't get response on browser浏览器没有反应
【发布时间】:2022-01-21 06:58:39
【问题描述】:

我编写了一个代码来读取 json 文件并将其打印到浏览器。代码工作正常,它在控制台上打印数据。但没有上浏览器。

app.post("/uploadfile",function (req, res, next) {
    
// Error MiddleWare for multer file upload, so if any
// error occurs, the image would not be uploaded!
upload(req,res,function(err) {

    if(err) {
        res.send(err)
    }
    else {

        //res.sendFile(__dirname+"/result.html")

        // Read users.json file
        fs.readFile(__dirname+"/uploads/zipFile.json", function(err, data) {

        // Check for errors
        if (err) throw err;

        // Converting to JSON
        const msg = JSON.parse(data);

        console.log(msg)

        app.post('/', function(req, res){
        res.send(msg);

【问题讨论】:

    标签: javascript node.js json web-deployment nodejs-server


    【解决方案1】:

    在我看来,您的问题出在app.post('/', function(req, res){ 行。

    请删除该行并测试它是否有效。

    app.post("/uploadfile",function (req, res, next) {
        
    // Error MiddleWare for multer file upload, so if any
    // error occurs, the image would not be uploaded!
    upload(req,res,function(err) {
    
        if(err) {
            res.send(err)
        }
        else {
    
            //res.sendFile(__dirname+"/result.html")
    
            // Read users.json file
            fs.readFile(__dirname+"/uploads/zipFile.json", function(err, data) {
    
            // Check for errors
            if (err) {
              res.send(err);
              return;
            }
    
            // Converting to JSON
            const msg = JSON.parse(data);
    
            console.log(msg)
    
            res.send(msg);
    
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      • 2016-05-28
      • 2017-07-13
      • 2020-06-06
      相关资源
      最近更新 更多