在POST
之后得到db:
[
{
"_id": "5420ccba19371ce81026cbdd",
"__v": 0
}
]
表示模型
var mongoose = require( 'mongoose' ),
Schema = mongoose.Schema;
var problemSchema = new Schema({
title: String,
grade: String,
author: String
});
module.exports = mongoose.model('Problem', problemSchema);
路由器:
router.route( '/problems' )
// get user list
.get( function ( req, res ) {
Problem.find( function ( err, problems ) {
if (err) {
return res.send( err );
}
res.json ( problems );
}
);
})
// create a user
.post( function ( req, res ) {
var problem = new Problem( req.body );
console.log( JSON.stringify( req.body ) );
console.log( JSON.stringify( problem ) );
problem.save( function ( err ) {
if ( err ) {
return res.send( err );
}
res.send( { message: 'Problem Added' } );
});
});
应用程式:
var express = require( 'express' ),
bodyParser = require( 'body-parser' ),
mongoose = require( 'mongoose' );
// routes
// var users = require( './routes/users' ),
var problems = require( './routes/problems' );
var app = express(); //Create the Express app
//connect to our database
//Ideally you will obtain DB details from a config file
var db = 'test';
var connectionString = 'mongodb://localhost:27017/' + db;
// connection func
mongoose.connect( connectionString );
// serve static files / client application
app.use( express.static( __dirname + '/public' ) );
//configure body-parser
app.use( bodyParser.json() );
app.use( bodyParser.urlencoded() );
//This is our route middleware
// app.use( '/api', users );
app.use( '/api', problems );
module.exports = app;
为什么?
ps postman POST
: