我有一个简单的工作NodeJS测试应用程序,包括express,mongodb和mongoose。至于现在,我试图清理我的代码并找到最佳实践。我的一个步骤是提取REST路线,但我在这方面遇到了问题,我也很困惑,可以做到这一点的方式,2016年最好的方法是什么?
我的代码(app.js):
"use strict"
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const Subjects = require('./models/Subjects').model('Subjects');
const Students = require('./models/Students').model('Students');
const app = express();
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
mongoose.connect('mongodb://localhost/schoolapp');
app.get('/subjects', function(req, res) {
Subjects.find({})
.exec(function(err, subjects) {
if(err) {
res.send('error msg has occured');
} else {
res.json(subjects);
}
})
});
app.put('/subjects', function(req, res) {
const query = {name: req.body.name};
const update = {$inc: {votes: 1}};
Subjects.findOneAndUpdate(query, update,
function(err, newSubject) {
if(err) {
console.log('An error occured');
} else {
res.send(newSubject);
}
})
})
app.post('/subjects', function(req, res) {
let newSubject = new Subjects();
newSubject.name = req.body.name;
newSubject.level = req.body.level;
newSubject.votes = 0;
newSubject.save(function(err, subject) {
if(err) {
res.send('error saving subject');
} else {
res.send(subject);
}
})
});
app.get('/students', function(req, res) {
Students.find({})
.exec(function(err, students) {
if(err) {
res.send('error saving students');
} else {
res.send(students);
}
})
})
app.post('/students', function(req, res) {
let newStudent = new Students();
newStudent.name = req.body.name;
newStudent.save(function(err, student) {
if(err) {
res.send('error saving student');
} else {
res.send(student);
}
})
})
app.put('/students', function(req, res) {
const query = {"_id": req.body.id};
const update = {$push: {subjects: req.body.name}};
Students.findOneAndUpdate(query, update,
function(err, newSubject) {
if(err) {
res.send('something went wrong');
} else {
res.send(newSubject);
}
});
})
app.listen(3000, function() {
console.log('app listening on port 3000');
});
我想将/students
和/subjects
解压缩为单独的文件,并在我的app.js中要求它们。
我尝试过3种不同结果的方法,包括:
module.exports = function(app) {
app.get('/students', ...)
}
并在app.js中:
var students = require(./rest/students)(app);
非常感谢:)
答案 0 :(得分:0)
由于mh-cbon,我已将其整理出来了。我只是想在这里发布我的解决方案,以防其他人在寻找问题。
app.js
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const app = express();
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use('/subjects', require('./rest/subjects'));
app.use('/students', require('./rest/students'));
mongoose.connect('mongodb://localhost/schoolapp');
app.listen(3000, function() {
console.log('app listening on port 3000');
});
students.js
'use strict';
const Students = require('../models/Students').model('Students');
const express = require('express');
let router = express.Router();
router.get('/', function(req, res) {
Students.find({})
.exec(function(err, students) {
if(err) {
res.send('error saving students');
} else {
res.send(students);
}
})
})
router.post('/', function(req, res) {
let newStudent = new Students();
newStudent.name = req.body.name;
newStudent.save(function(err, student) {
if(err) {
res.send('error saving student');
} else {
res.send(student);
}
})
})
router.put('/', function(req, res) {
const query = {"_id": req.body.id};
const update = {$push: {subjects: req.body.name}};
Students.findOneAndUpdate(query, update,
function(err, newSubject) {
if(err) {
res.send('something went wrong');
} else {
res.send(newSubject);
}
});
})
module.exports = router;
注意:通过./rest/subjects
指定app.js中的路径将使模块根目录'/'
中的路径(请参阅students.js),与某些教程不同。
我也发现这篇文章很有用(因为它显示了一个简单的例子): https://www.terlici.com/2014/09/29/express-router.html