时间:2021-07-01 10:21:17 帮助过:2人阅读
数据库定义:
/** * Created by sahara on 2016/7/5. */ var mongoose = require(‘mongoose‘); var openingTimeSchema = new mongoose.Schema({ days: {type: String, required: true}, opening: String, closing: String, closed: {type: Boolean, required: true} }); var reviewSchema = new mongoose.Schema({ author:String, rating: {type: Number, required: true, min: 0, max: 5}, reviewText: String, createdOn: {type: Date, "default": Date.now} }); var locationSchema = new mongoose.Schema({ name: {type: String, required: true}, address: String, rating: {type: Number, "default": 0, min: 0, max: 5}, facilities: [String], coords: {type: [Number], index: ‘2dsphere‘}, openingTimes: [openingTimeSchema], reviews: [reviewSchema] }); mongoose.model(‘Location‘, locationSchema);
路由层写法:
var express = require(‘express‘); var router = express.Router(); var ctrlLocations = require(‘../controllers/locations‘); var ctrlReviews = require(‘../controllers/reviews‘); //locations router.get(‘/locations‘, ctrlLocations.locationsListByDistance); router.post(‘/locations‘, ctrlLocations.locationsCreate); router.get(‘/locations/:locationid‘, ctrlLocations.locationsReadOne); router.put(‘/locations/:locationid‘, ctrlLocations.locationsUpdateOne); router.delete(‘/locations/:locationid‘, ctrlLocations.locationsDeleteOne); //reviews router.post(‘/locations/:locationid/reviews‘, ctrlReviews.reviewCreate); router.get(‘/locations/:locationid/reviews/:reviewid‘, ctrlReviews.reviewReadOne); router.put(‘/locations/:locationid/reviews/:reviewid‘, ctrlReviews.reviewUpdateOne); router.delete(‘/locations/:locationid/reviews/:reviewid‘, ctrlReviews.reviewDeleteOne); module.exports = router
控制器写法:
/** * Created by sahara on 2016/7/6. */ var mongoose = require(‘mongoose‘); var Loc = mongoose.model(‘Location‘); var sendJsonResponse = function(res, status, content) { res.status(status); res.json(content); }; module.exports.locationsListByDistance = function (req, res) { sendJsonResponse(res, 200, {"status" : "success"}); }; module.exports.locationsCreate = function (req, res) { sendJsonResponse(res, 201, {"status" : "success"}); }; module.exports.locationsReadOne = function (req, res) { if (req.params && req.params.locationid){ Loc .findById(req.params.locationid) .exec(function(err, location){ if (!location) { sendJsonResponse(res, 404, { "message": "locationid not found" }); return; } else if (err) { sendJsonResponse(res, 404, err); return; } sendJsonResponse(res, 200, location); }); } else { sendJsonResponse(res, 404, { "message": "no locationid in request" }); } }; module.exports.locationsUpdateOne = function (req, res) { sendJsonResponse(res, 200, {"status" : "success"}); }; module.exports.locationsDeleteOne = function (req, res) { sendJsonResponse(res, 204, {"status" : "success"}); };
app.js的改装:
var express = require(‘express‘); var path = require(‘path‘); var favicon = require(‘serve-favicon‘); var logger = require(‘morgan‘); var cookieParser = require(‘cookie-parser‘); var bodyParser = require(‘body-parser‘); require(‘./app_api/models/db‘); var routes = require(‘./app_server/routes/index‘); var routesApi = require(‘./app_api/routes/index‘); var users = require(‘./app_server/routes/users‘); var app = express(); // view engine setup app.set(‘views‘, path.join(__dirname, ‘app_server‘, ‘views‘)); app.set(‘view engine‘, ‘jade‘); // uncomment after placing your favicon in /public //app.use(favicon(path.join(__dirname, ‘public‘, ‘favicon.ico‘))); app.use(logger(‘dev‘)); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, ‘public‘))); app.use(‘/‘, routes); app.use(‘/api‘, routesApi); app.use(‘/users‘, users); // catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error(‘Not Found‘); err.status = 404; next(err); }); // error handlers // development error handler // will print stacktrace if (app.get(‘env‘) === ‘development‘) { app.use(function(err, req, res, next) { res.status(err.status || 500); res.render(‘error‘, { message: err.message, error: err }); }); } // production error handler // no stacktraces leaked to user app.use(function(err, req, res, next) { res.status(err.status || 500); res.render(‘error‘, { message: err.message, error: {} }); }); module.exports = app;
MEAN,从MONGO DB里弄出点东东来啦,以REST风格显示JSON
标签: