我正在构建一个node.js应用程序,它是一个REST api,使用express和mongoose为我的mongodb.我现在已经完成了所有设置的CRUD端点,但我只想知道两件事.
如何扩展这种路由方式,具体来说,如何在路由之间共享模块.我希望我的每个路由都进入一个新文件,但显然只有一个数据库连接,你可以看到我在people.js的顶部包含了mongoose.
我是否必须在我的people.js中写出模型的模式3次?第一个模式定义了模型,然后我列出了createPerson和updatePerson函数中的所有变量.这感觉就像我在当天制作php/mysql CRUD一样哈哈.对于更新功能,我尝试编写一个循环来循环"p"以自动检测要更新的字段,但无济于事.任何提示或建议都会很棒.
此外,我喜欢整个应用程序的任何意见,对节点不熟悉,很难知道你做某事的方式是最有效或"最好"的做法.谢谢!
app.js
// Node Modules
var express = require('express');
app = express();
app.port = 3000;
// Routes
var people = require('./routes/people');
/*
var locations = require('./routes/locations');
var menus = require('./routes/menus');
var products = require('./routes/products');
*/
// Node Configure
app.configure(function(){
app.use(express.bodyParser());
app.use(app.router);
});
// Start the server on port 3000
app.listen(app.port);
/*********
ENDPOINTS
*********/
// People
app.get('/people', people.allPeople); // Return all people
app.post('/people', people.createPerson); // Create A Person
app.get('/people/:id', people.personById); // Return person by id
app.put('/people/:id', …Run Code Online (Sandbox Code Playgroud)