var express = require('express');
var path = require('path');
var app = express();
var mongoose = require('mongoose');
var bodyparser = require('body-parser');
mongoose.Promise = require('bluebird');
mongoose.connect("mongodb://makja:q1w2e3r4@ds111123.mlab.com:11123/makja" , { useMongoClient: true });
var db = mongoose.connection;
db.once("open", function () {
console.log("DB on!");
});
db.on("error", function (err) {
console.log("DB error : " , err);
});
var dataSchema = mongoose.Schema({
name:String,
count:Number
});
var postSchema = mongoose.Schema({
title : {type:String, required:true},
body : {type:String, required:true},
createdAt : {type:Date, default:Date.now},
updatedAt : Date
});
var Post = mongoose.model('post',postSchema);
app.get('/posts', function(req , res){
Post.find({}, function(err,posts){
if (err) return res.json({success:false, message:err});
res.json({success:true, data:posts});
});
});
app.post('/posts', function(req , res){
Post.create(req.body.post, function(err,post){
if (err) return res.json({success:false, message:err});
res.json({success:true, data:post});
});
});
var Data = mongoose.model('data',dataSchema);
Data.findOne({name:"myData"},function(err,data){
if (err) return console.log("Data error",err);
if (!data) {
Data.create({name:"myData",count:0},function(err,data){
if (err) return console.log("Data error:",err);
console.log("counter initalized:",data);
});
}
});
app.set("view engine",'ejs');
app.use(express.static(path.join(__dirname,'public')));
app.use(bodyparser.json());
app.get('/',function(req , res) {
Data.findOne({name:"myData"}, function(err,data){
if(err) return console.log("Data error: ",err);
data.count++;
data.save(function(err){
if(err) return console.log("Data error: ",err);
res.render('my_first_ejs',data);
});
});
});
app.get('/',function (req,res) {
res.render('my_first_ejs');
});
app.listen(8000, function(){`enter code here`
console.log('Server On!');
});
cmd错误-------------------------------
TypeError: Cannot read property 'post' of undefined
at C:\nodejs\index.js:38:23
at Layer.handle [as handle_request] (C:\nodejs\node_modules\express\lib\router\layer.js:95:5)
at next (C:\nodejs\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\nodejs\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\nodejs\node_modules\express\lib\router\layer.js:95:5)
at C:\nodejs\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\nodejs\node_modules\express\lib\router\index.js:335:12)
at next (C:\nodejs\node_modules\express\lib\router\index.js:275:10)
at expressInit (C:\nodejs\node_modules\express\lib\middleware\init.js:40:5)
at Layer.handle [as handle_request] (C:\nodejs\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\nodejs\node_modules\express\lib\router\index.js:317:13)
at C:\nodejs\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\nodejs\node_modules\express\lib\router\index.js:335:12)
at next (C:\nodejs\node_modules\express\lib\router\index.js:275:10)
at query (C:\nodejs\node_modules\express\lib\middleware\query.js:44:5)
at Layer.handle [as handle_request] (C:\nodejs\node_modules\express\lib\router\layer.js:95:5)
答案 0 :(得分:0)
确保你的bodyparser实际上是在解决你的POST之前正在解析body并将body连接到req。
- > app.use(bodyparser.json());
var app = require('express')();
var bodyParser = require('body-parser');
app.post('/', (req, res) =>{
console.log(req.body); //undefined
res.json(req.body).status(200);
})
app.use(bodyParser.json());
app.listen(3000);
====
var app = require('express')();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.post('/', (req, res) =>{
console.log(req.body); //attaches correctly
res.json(req.body).status(200);
})
app.listen(3000);