process.nextTick(function(){throw err;})rror:getaddrinfo ENOTFOUND noder noder:27017 - nodejs

时间:2016-07-28 12:22:00

标签: node.js nodes

当我第一次点击http://localhost:8080时,它会给我正确的回复,但也会显示以下错误。当再次点击http://localhost:8080时,我在UI上看不到任何内容。

D:\node_workspace\node-token-jwt\node_modules\mongodb\lib\server.js:242
        process.nextTick(function() { throw err; })
                                      ^
Error: getaddrinfo ENOTFOUND noder noder:27017
    at errnoException (dns.js:27:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:78:26)

我正在使用node.js.我该如何解决这个问题?

server.js

// get the packages 
var express     = require('express');
var app         = express();
var bodyParser  = require('body-parser');
var morgan      = require('morgan');
var mongoose    = require('mongoose');

var jwt    = require('jsonwebtoken'); // used to create, sign, and verify tokens
var config = require('./config'); // get our config file
var User   = require('./app/models/user'); // get our mongoose model


var port = process.env.PORT || 8080; // used to create, sign, and verify tokens
mongoose.connect(config.database); // connect to database
app.set('superSecret', config.secret); // secret variable

// use body parser so we can get info from POST and/or URL parameters
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// use morgan to log requests to the console
app.use(morgan('dev'));

app.get('/', function(req, res) {
    res.send('Hello! The API is at http://localhost:' + port + '/api');
});


app.listen(port);
console.log('Magic happens at http://localhost:' + port);

user.js的

// get an instance of mongoose and mongoose.Schema
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

// set up a mongoose model and pass it using module.exports
module.exports = mongoose.model('User', new Schema({ 
    name: String, 
    password: String, 
    admin: Boolean 
}));

config.js

module.exports = {
    'secret': 'ilovescotchyscotch',
    'database': 'mongodb://noder:noderauth&54;proximus.modulusmongo.net:27017/test'
};

的package.json

{
  "name": "node-token-jwt",
  "main": "server.js",
  "dependencies": {
    "body-parser": "^1.15.2",
    "express": "^4.14.0",
    "jsonwebtoken": "^7.1.6",
    "mongoose": "^4.5.7",
    "morgan": "^1.7.0"
  }
}

1 个答案:

答案 0 :(得分:0)

Mongodb似乎没有运行

尝试

键入“ mongod”,然后重新启动服务器