发送发布请求后,Node js应用程序未与mongodb连接吗?

时间:2019-04-10 23:15:15

标签: node.js mongodb heroku

heroku与mongodb地图集连接时出现问题。它的超时请求。我还将0.0.0.0/0设置为从任何IP访问。但是我仍然面临超时的问题

const Express = require("express");const Mongoose = require("mongoose");
const BodyParser = require("body-parser");
var app = Express();
app.use(BodyParser.json());
app.use(BodyParser.urlencoded({ extended: true }));

Mongoose.connect('mongodb+srv://nsadfsfufdr:necsfdstsadfur@cluster0-hwqfq.mongodb.net/nectur?retryWrites=true',{ useNewUrlParser: true });
const StoreModel = Mongoose.model("stores", {
    storename: String,
    managername: String,
    address: String,
    lat: Number,
    lng: Number,
    phone: String
});



app.post("/person", async (request, response) => {

        /*var person = new PersonModel(request.body);
        var result = await person.save();
        response.send(result);
*/

});
app.get("/people", async (request, response) => {});
app.get("/", async (request, response) => {

    try {
        response.send('Hello World');
    } catch (error) {
        response.status(500).send(error);
    }

});


app.get("/store", async (request, response) => {
    try {
        var result = await StoreModel.find().exec();
        response.send(result);
    } catch (error) {
        response.status(500).send(error);
    }
});



app.post("/addstore",  (request, response) => {





 /*   app.post("/addname", (req, res) => {
 var myData = new User(req.body);
 myData.save()
 .then(item => {
 res.send("item saved to database");
 })
 .catch(err => {
 res.status(400).send("unable to save to database");
 });
});



*/



        console.log(request.body.data);
        var store = new StoreModel(request.body.data);


     store.save().then(result=>{response.send(result)}).catch(err=> {response.status(400).send(err);});

  /*  

        var result =  store.save();

        console.log(result);
        response.send(result);
        response.status(500).send(error);
    */

});
app.get("/person/:id", async (request, response) => {});
app.put("/person/:id", async (request, response) => {});
app.delete("/person/:id", async (request, response) => {});

app.listen(process.env.PORT || 3000, () => {
    console.log("Listening at :3000...");
});

Mongodb从本地连接到mongodb地图集,但仅在从heroku连接时才面临问题

Heroku日志

2019-04-10T22:55:36.975665+00:00 app[web.1]:   phone: '234234' }
2019-04-10T22:56:06.976879+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/addstore" host=nectur.herokuapp.com request_id=6c04b3e7-8cf9-49d4-856b-d90e7d8bc1cb fwd="117.96.207.200" dyno=web.1 connect=0ms service=30000ms status=503 bytes=0 protocol=https
2019-04-10T22:57:16.367113+00:00 heroku[router]: at=info method=GET path="/" host=nectur.herokuapp.com request_id=e1a78074-73ff-42c5-9873-e2c8c97297de fwd="117.96.207.200" dyno=web.1 connect=1ms service=5ms status=304 bytes=149 protocol=https
2019-04-10T23:00:26.235103+00:00 app[web.1]: { storename: 'Murugan',
2019-04-10T23:00:26.235119+00:00 app[web.1]:   managername: 'Murugan',
2019-04-10T23:00:26.235121+00:00 app[web.1]:   address: 'String',
2019-04-10T23:00:26.235123+00:00 app[web.1]:   lat: '-23.23',
2019-04-10T23:00:26.235125+00:00 app[web.1]:   lng: '89',
2019-04-10T23:00:26.235127+00:00 app[web.1]:   phone: '234234' }
2019-04-10T23:00:56.234732+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/addstore" host=nectur.herokuapp.com request_id=e0cb6c17-960e-4304-8a81-35f1321e600e fwd="117.96.207.200" dyno=web.1 connect=0ms service=30000ms status=503 bytes=0 protocol=https

0 个答案:

没有答案