-1
我在寫一個簡單的post調用。我的代碼如下:在Express中執行POST查詢時連接ETIMEDOUT錯誤js
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080;
var router = express.Router();
var mongoose = require('mongoose');
mongoose.connect('mongodb://node:[email protected]:27017/Iganiq8o');
router.use(function(req, res, next) {
console.log('Something is happening.');
next();
});enter code here
router.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our api!' });
});
var Bear = require('./app/models/bears');
router.route('/bears')
.post(function(req, res) {
var bear = new Bear();
bear.name = req.body.name;
bear.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Bear created!' });
});
});
app.use('/api', router);
app.listen(port);
console.log('Magic happens on port ' + port);
我以前收到以下錯誤
failed to connect to [novus.modulusmongo.net:27017]
我通過更新貓鼬糾正,但現在還是我得到另一個錯誤是:
/node_modules/mongoose/node_modules/mongodb/lib/server.js:242
process.nextTick(function() { throw err; })
^
錯誤:連接ETIMEDOUT 54.209.84.209:27017
是蒙戈服務器上運行?在啓動連接之前,服務應該正在運行。 –
您在代碼中給出的URL看起來像一個遠程數據庫。你確定你有權訪問它並且正在運行?嘗試在本地運行數據庫。 –
是的,我已經事先啓動了mongo服務器 –