我正在使用nodejs制作一个安静的API,当我使用mongodb上的x-www-form-urlencoded数据的方法在邮递员上输入数据时为空。当我在数据中使用raw时输入邮递员。怎么了?谢谢。这是我的代码。请帮助我..
App.js
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var morgan = require('morgan');
var path = require('path');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var http = require('http');
var logger = require('morgan');
var favicon = require('serve-favicon');
var session = require('express-session');
var multer = require('multer');
var errorhandler = require('errorhandler');
var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(methodOverride());
app.use(session({
resave: true,
saveUninitialized: true,
secret: 'uwotm8'
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(multer());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'bower_component')));
app.use(passport.initialize());
app.use(passport.session());
var indexRoute = require('./routes/IndexRoute');
var userRoute = require('./routes/UserRoute');
var loginRoute = require('./routes/LoginRoute');
app.use('/', indexRoute);
app.use('/api/user', userRoute);
app.use('/', loginRoute);
app.all('/*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', req.headers.origin || '*');
res.header('Access-Control-Allow-Headers', 'X-Request-With, Content-Type');
res.header('Access-Control-Allow-Methods', 'GET');
next();
});
mongoose.connect('mongodb://localhost/Belajar', function(err, res) {
if (err) {
console.log('Koneksi mongodb gagal');
} else {
console.log('Koneksi mongodb berhasil');
}
});
if ('development' == app.get('env')) {
app.use(errorhandler());
}
var server = http.createServer(app);
server.listen(app.get('port'), function() {
console.log('Server jalan di port : ' + app.get('port'));
});
Route
UserRoute.js
var express = require('express');
var mongoose = require('mongoose');
var User = require('../models/User');
var router = express.Router();
var getUser = function(req, res, next) {
User.find(function(err, users) {
if (err) return res.json(err);
return res.json({
user: users
});
});
};
var getUserObject = function(req, res, next) {
User.findOne({
email: req.params.email
}, function(err, user) {
if (err) return res.json(err);
if (!user) return res.json({
info: '404',
pesan: 'Data tidak ada'
});
return res.json(user);
});
};
var saveUser = function(req, res, next) {
var user = new User({
email: req.body.email,
password: req.body.password,
nama: req.body.nama,
noHp: req.body.noHp,
tanggalLahir: req.body.tanggalLahir,
role: req.body.role
});
user.save(function(err) {
if (err) return res.json(err);
return res.json({
success: 'true',
pesan: 'Data Tersimpan'
});
});
};
var editUser = function(req, res, next) {
return User.update({
email: req.params.email
}, req.body, function(err) {
if (err) return res.json(err);
return res.json({
success: 'true',
pesan: 'Data Berhasil di edit'
});
});
};
var deleteUser = function(req, res, next) {
return User.remove({
email: req.params.email
}, function(err) {
if (err) return res.json(err);
return res.json({
success: 'true',
pesan: 'Data Berhasil di hapus'
});
});
}
router.get('/', getUser);
router.get('/find/:email', getUserObject);
router.post('/save', saveUser);
router.put('/update/:email', editUser);
router.delete('/delete/:email', deleteUser);
module.exports = router;
Model
User.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var User = new Schema(
{
email: {
type: 'String',
require: true
},
password: {
type: 'String',
require: true
},
nama: {
type: 'String',
require: true
},
noHp: {
type: 'String',
require: true
},
tanggalLahir: {
type: 'String',
require: true
},
role: {
type: 'String',
require: true
}
},
{
collection: 'tb_user'
}
);
module.exports = mongoose.model('User', User);
package.json
{
"name": "Belajar-Express",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "lalala",
"license": "ISC",
"dependencies": {
"body-parser": "^1.12.3",
"connect": "^3.3.5",
"ejs": "^2.3.1",
"errorhandler": "^1.3.5",
"express": "^4.12.3",
"express-session": "^1.11.1",
"method-override": "^2.3.2",
"mongoose": "^4.0.2",
"morgan": "^1.5.2",
"multer": "^0.1.8",
"passport": "^0.2.1",
"passport-local": "^1.0.0",
"serve-favicon": "^2.2.0"
}
}