0

我刚刚在 WebStorm 中创建了一个简单的项目,Express module 然后我安装mongoose,之后我尝试连接到 MongoDB,但它给出了这个异常:

索引.js

var mongoose = require('mongoose');
mongoose.connect('localhost', 'Test');
var schema = mongoose.Schema({ name: 'string' },{age:'int'});
var Human = mongoose.model('Human', schema);

 exports.saveHuman = function (req,res){
"use strict";
var Ahs = new Human({name:'Dumy'},{age:24});
Ahs.save(function(error , data ){

    if(error){
        console.log("Not working");
    }
    else{
        res.send(Ahs.name + "Created !");
    }

});
};

 exports.index = function(req, res){
 "use strict";
res.render('index', { title: 'Express' });
 };

应用程序.js

var express, routes, user, http, path;
express = require('express');
routes = require('./routes');
user = require('./routes/user');
http = require('http');
path = require('path');

var app = express();

 app.configure(function(){
"use strict";
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
"use strict";
app.use(express.errorHandler());
 });
app.get('/saveHuman',routes.saveHuman);
app.get('/', routes.index);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
"use strict";
console.log("Express server listening on port " + app.get('port'));
 });

现在,当我运行项目时,它会在 console 中显示。

在此处输入图像描述

4

0 回答 0