gpt4 book ai didi

node.js - [错误 : failed to connect to [localhost:27017]] . 此错误发生在 mongodb 上

转载 作者:可可西里 更新时间:2023-11-01 09:31:21 24 4
gpt4 key购买 nike

<分区>

我的示例代码在这里。我正在从书 developing_backbone.js_applications 中学习我已经在我的电脑上安装了 mongodb。我在这个网站上搜索了答案,但没有任何线索。有没有人有任何解决方案?谢谢!

// Module dependencies.
var application_root = __dirname,
express = require('express'), //Web framework
path = require('path'), //Utilities for dealing with file paths
mongoose = require('mongoose'); //MongoDB integration
//Create server
var app = express();
// Configure server
app.configure(function () {
//parses request body and populates request.body
app.use(express.bodyParser());
//checks request.body for HTTP method overrides
app.use(express.methodOverride());
//perform route lookup based on URL and HTTP method
app.use(app.router);
//Where to serve static content
app.use(express.static(path.join(application_root, 'site')));
//Show all errors in development
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});


//Start server
var port =4711;
app.listen(port, function () {
console.log('Express server listening on port %d in %s mode',
port, app.settings.env);
});

// Routes
app.get( '/api', function( request, response ) {

response.send( 'Library API is running and the requestis' );
});

//Connect to database
mongoose.connect( 'mongodb://localhost/library_database', function(err) { if (err) console.log(err); } );
//Schemas
var Book = new mongoose.Schema({
title: String,
author: String,
releaseDate: Date
});
//Models
var BookModel = mongoose.model( 'Book', Book );

//Get a list of all books
app.get( '/api/books', function( request, response ) {
return BookModel.find( function( err, books ) {
if( !err ) {
return response.send( books );
} else {
return console.log( err );
}
});
});

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com