应用在localhost上完美运行,但在部署到heroku后引发错误

时间:2020-04-14 15:34:07

标签: javascript node.js express heroku


const express = require("express");
const bodyParser = require("body-parser");
const https = require("https");

const app = express();
app.use(express.static(__dirname+"/public"));
app.use(bodyParser.urlencoded({extended:true}));

app.get("/", function(req,res){
    res.sendFile(__dirname+"/signup.html");
});

app.post("/", function(req,res){
    const firstName = req.body.fName;
    const  lastName = req.body.lName;
    const email = req.body.email;
    const data = {
        members:[
            {
                email_address:email,
                status:"subscribed",
                merge_fields:{
                    FNAME:firstName,
                    LNAME:lastName
                }

            }
        ]
    };

    const jsonData = JSON.stringify(data);
    const url = "https://us19.api.mailchimp.com/3.0/lists/listkey";
    const options= {
        method:"POST",
        auth:"username23:apikey-us45"
    }

   const request =  https.request(url, options, function(resonse){
       if(resonse.statusCode === 200){
           res.sendFile(__dirname+"/success.html");
       }
       else{
           res.sendFile(__dirname+"/failure.html")
       }
        resonse.on("data",function(data){
            console.log(JSON.parse(data));
        });
     });

     request.write(jsonData);
     request.end();

});

app.post("/failure",function(req,res){
    res.redirect("/");
});

app.listen(process.env.PORT || 3000, function(){
       console.log("Server running on port 3000")

});

此代码在本地运行良好,但在部署到heroku之后;什么时候 我单击我的提交按钮,以便我的app.js可以处理我的数据并 使用mailchimp api向mailchimp发送请求...它一直显示 浏览器和heroku日志上的“内部服务器错误”报告了此情况。

 2020-04-13T17:04:03.068336+00:00 app[web.1]: TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type Function. Received type object
2020-04-13T17:04:03.068352+00:00 app[web.1]: at ClientRequest.once (events.js:288:11)
2020-04-13T17:04:03.068353+00:00 app[web.1]: at new ClientRequest (_http_client.js:141:10)
2020-04-13T17:04:03.068354+00:00 app[web.1]: at Object.request (https.js:272:10)
2020-04-13T17:04:03.068355+00:00 app[web.1]: at /app/app.js:39:27
2020-04-13T17:04:03.068356+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2020-04-13T17:04:03.068357+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/route.js:137:13)
2020-04-13T17:04:03.068358+00:00 app[web.1]: at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
2020-04-13T17:04:03.068358+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2020-04-13T17:04:03.068359+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:281:22
2020-04-13T17:04:03.068359+00:00 app[web.1]: at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)
2020-04-13T17:04:03.071903+00:00 heroku[router]: at=info method=POST path="/" host=hidden-plains-53682.herokuapp.com request_id=4bfa3f01-fd24-44f2-bf53-f9a619c92b7c fwd="105.112.31.157" dyno=web.1 connect=0ms service=24ms status=500 bytes=404 protocol=https
2020-04-13T17:37:36.689688+00:00 heroku[web.1]: Idling
2020-04-13T17:37:36.693286+00:00 heroku[web.1]: State changed from up to down

0 个答案:

没有答案