API使用Express,NodeJS,MongoDB,数据不填充

时间:2015-12-24 02:40:48

标签: javascript node.js mongodb api express

我正在尝试使用Node.js,Express和MongoDB创建Rest API。我目前在我的本地主机上运行:3000。当我尝试重新启动并运行服务器时,我正在使用路由http://localhost:3000/drinks

我使用Postman发送HTTP请求。 https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en

尝试发送上述路线时,它不会检索任何信息。它只是继续加载。似乎数据没有填充。

这是我第一次创建REST API,但我不确定它为什么不检索数据。下面是我的代码。提前谢谢!

server.js

var express = require('express'),
    drink = require('./routes/drinks');

var app = express();

app.configure(function () {
        app.use(express.logger('dev'));     /* 'default', 'short', 'tiny', 'dev' */
        app.use(express.bodyParser());
    });

app.get('/drinks', function (req, res) {
  drink.findAll(req, res);
});
app.get('/drinks/:id', function (req, res) {
  drink.findById(req, res);
});                                                                     

app.listen(3000);
console.log('Listening on port 3000...');

drinks.js

var mongo = require('mongodb');

var Server = mongo.Server,
    Db = mongo.Db,
    BSON = mongo.BSONPure;

var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('drinkdb', server);

db.open(function(err, db) {
        if(!err) {
            console.log("Connected to 'drinkdb' database");
            db.collection('drinks', {strict:true}, function(err, collection) {
                    if (err) {
                        console.log("The 'drinks' collection doesn't exist. Creating it with sample data...");
                        populateDB();
                    }
                });
        }
    });

exports.findById = function(req, res) {
    var id = req.params.id;
    console.log('Retrieving drink: ' + id);
    db.collection('drinks', function(err, collection) {
            collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item) {
                    res.send(item);
                });
        });
};

exports.findAll = function(req, res) {
    db.collection('drinks', function(err, collection) {
            collection.find().toArray(function(err, drinks) {
                    res.send(drinks);
                });
        });
};
/*---------------------------------------------------------------------------------------------------------------*/
// Populate database with sample data -- Only used once: the first time the application is started.                   
// You'd typically not find this code in a real-life app, since the database would already exist.                     
var populateDB = function() {

    var drinks = [
    {
            id: "1",
            name: "Margarita",
            ingredients: ["Tequila","Lime juice","Triple Sec","Lime","Salt","Ice"],
            measurements: ["2 oz","1 oz","1 oz","1","optional","optional"],
            directions: "Shake the other ingredients with ice, then carefully pour into the glass. Served: On the roc\
ks; poured over ice. Optional: Salt the rim of the glass by rubbing lime on it so it sticks."
    },
   {
            id: "2",
            name: "Strawberry Margarita",
            ingredients: ["Tequila", "Lime juice","Triple Sec","Strawberries","Lime","Salt", "Ice"],
            measurements: ["2 oz","1 oz", "1 oz", "3 1/2 cups", "1", "optional", "optional"],
            directions: "Combine strawberries, ice, tequila, lime juice, and triple sec in a blender, and process unt\
il the mixture is smooth. Carefully pour into the glass. Served: On the rocks; poured over ice. Optional: Salt the ri\
m of the glass by rubbing lime on it so it sticks."
   }];

    db.collection('drinks', function(err, collection) {
            collection.insert(drinks, {safe:true}, function(err, result) {});
        });
};

警告是:

express deprecated app.configure: Check app.get('env') in an if statement server.js:6:5
connect deprecated multipart: use parser (multiparty, busboy, formidable) npm module instead node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:56:20
connect deprecated limit: Restrict request size at location of read node_modules/express/node_modules/connect/lib/middleware/multipart.js:86:15

0 个答案:

没有答案