admin管理员组

文章数量:1418619

I'm curious if anyone could provide some insight on the best way to abstract an API built with Node.js + Restify + Mongoose. After ing from an MVC / PHP background, it's interesting to find that there's not string/defined structure for Node applications.

As of now, I have my app.js file that auto loads my routes.js file, all model js files, etc.

The confusion is primarily in how my routes are supposed to interact with data from Mongo. Here is a basic rundown on how my code is layed out.

app.js:

/**
 * Require Dependencies
 */
var restify    = require('restify')
    , mongoose = require('mongoose')
    , config   = require('./config')
    , routes   = require('./routes');

/**
 * Create Server & Define Settings
 */
var server = restify.createServer({
    name: config.name,
    version: config.version
});

/**
 * Common Handlers
 */
server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.use(restify.jsonp());

/**
 * Connect to Mongo Database
 */
 mongoose.connect(config.mongo.uri, function(err) {

    // assign connection to var so we can pass it down the chain
    var db = mongoose.connection;

    // handle connection error
    db.on('error', console.error.bind(console, 'connection error:'));

    // handle connection success
    db.once('open', function callback () {

        /**
         * Start Routing API Calls
         */
        routes.route(server, db);

    });

});

/**
 * Start Server & Bind to Port
 */
server.listen(config.port, function () {
    console.log('%s v%s listening on port %s in %s mode.', server.name, server.version,          config.port, config.env);
});

routes.js:

module.exports.route = function(server, db) {

    var Users = require('./models/users.js');

    /**
     * Users
     */

    server.get('/users', function (req, res, next) {

        res.send(Users.list(db, req, res));

        return next();

    });

    server.get('/users/:user_id', function (req, res, next) {

        res.send(Users.get(db, req, res));

        return next();

    });

}  

models/users.js:

// fake database
var users = [
    {
        name: 'Nick P',
        email: '[email protected]'
    },
    {
        name: 'Zack S',
        email: '[email protected]'
    }
];

exports.list = function(db, req, res) {

return users;

};

exports.get = function(db, req, res) {

    return users[req.params.user_id];

};

As you can see, I'm using a "fake database" that is a simple object. Where / how could I introduce a Mongoose layer to municate with our database? I'm mostly concerned with how I should use schemas and exports. Any code examples, or direction would be awesome.

I'm curious if anyone could provide some insight on the best way to abstract an API built with Node.js + Restify + Mongoose. After ing from an MVC / PHP background, it's interesting to find that there's not string/defined structure for Node applications.

As of now, I have my app.js file that auto loads my routes.js file, all model js files, etc.

The confusion is primarily in how my routes are supposed to interact with data from Mongo. Here is a basic rundown on how my code is layed out.

app.js:

/**
 * Require Dependencies
 */
var restify    = require('restify')
    , mongoose = require('mongoose')
    , config   = require('./config')
    , routes   = require('./routes');

/**
 * Create Server & Define Settings
 */
var server = restify.createServer({
    name: config.name,
    version: config.version
});

/**
 * Common Handlers
 */
server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.use(restify.jsonp());

/**
 * Connect to Mongo Database
 */
 mongoose.connect(config.mongo.uri, function(err) {

    // assign connection to var so we can pass it down the chain
    var db = mongoose.connection;

    // handle connection error
    db.on('error', console.error.bind(console, 'connection error:'));

    // handle connection success
    db.once('open', function callback () {

        /**
         * Start Routing API Calls
         */
        routes.route(server, db);

    });

});

/**
 * Start Server & Bind to Port
 */
server.listen(config.port, function () {
    console.log('%s v%s listening on port %s in %s mode.', server.name, server.version,          config.port, config.env);
});

routes.js:

module.exports.route = function(server, db) {

    var Users = require('./models/users.js');

    /**
     * Users
     */

    server.get('/users', function (req, res, next) {

        res.send(Users.list(db, req, res));

        return next();

    });

    server.get('/users/:user_id', function (req, res, next) {

        res.send(Users.get(db, req, res));

        return next();

    });

}  

models/users.js:

// fake database
var users = [
    {
        name: 'Nick P',
        email: '[email protected]'
    },
    {
        name: 'Zack S',
        email: '[email protected]'
    }
];

exports.list = function(db, req, res) {

return users;

};

exports.get = function(db, req, res) {

    return users[req.params.user_id];

};

As you can see, I'm using a "fake database" that is a simple object. Where / how could I introduce a Mongoose layer to municate with our database? I'm mostly concerned with how I should use schemas and exports. Any code examples, or direction would be awesome.

Share Improve this question edited Feb 8, 2018 at 20:31 Cœur 38.8k25 gold badges206 silver badges278 bronze badges asked Jan 8, 2013 at 18:09 Nick ParsonsNick Parsons 8,60713 gold badges51 silver badges70 bronze badges 3
  • Just make Users.list use mongoose to access the database instead of just returning the fake database. Maybe I don't understand the question. – Chad Commented Jan 8, 2013 at 18:12
  • @Chad I figured it would be as simple as that. However, I'm also new to Mongoose and it looks like they require schema definitions. Wasn't positive on where I should put those [Mongoose Schemas] mongoosejs./docs/guide.html – Nick Parsons Commented Jan 8, 2013 at 19:42
  • Wherever you want, I usually have them as part of the model. – Chad Commented Jan 8, 2013 at 19:45
Add a ment  | 

1 Answer 1

Reset to default 7

Here a simple example of what I usually do with Express, it's kind of the same thing with Restify. You can manage your Mongoose schemas in the same way but in your Restify routes.

app.js :

var express = require('express');     
var app = express();

app.configure(function () {
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
});

// connection to mongoDB
var mongoose = require('mongoose');
mongoose.connect('mongodb:mongoURI');    

var user = require('./routes/users');

app.get('/users/list', user.list);

app.listen(3000);

models/user.js :

var mongoose = require('mongoose')
   ,Schema = mongoose.Schema
   ,ObjectId = Schema.ObjectId;

var userSchema = new Schema({
    id: ObjectId,
    name: {type: String, default: ''},
    email: {type: String, default: ''}
});

module.exports = mongoose.model('User', userSchema);

routes/users.js :

var User = require('../models/user.js');

exports.list = function(req, res) {
    User.find(function(err, users) {
        res.send(users);
    });
};

本文标签: javascriptAbstracting Mongoose with RestifyStack Overflow