Express应用程式-无法使用Postman测试Post请求 [英] `express` app - not able to test the `post` request using `postman`
本文介绍了Express应用程式-无法使用Postman测试Post请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个小型的 express
应用。我只是尝试测试 post
方法。但这根本没有用。根据我的说法,一切都很好。但我无法找出问题所在。有人帮我吗?
I have created a small express
app. I just trying to test the post
method. But it's not working at all. according to me all are fine. But I couln't figure out the issue. any one help me here please?
我的server.js文件:
my server.js file :
var express = require('express');
var bodyParser = require('body-parser');
var Post = require('./models/post');
var app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.get('/api/posts', function( req, res) {
res.json([
{
username:"dickeyxxx",
body : "node rocksxx"
}
])
});
app.post('/api/posts', function( req, res, next) {
var post = new Post({
username:req.body.username,
body:req.body.body
})
post.save(function(err, post){
if(err) { return next }
res.json(201,post);
})
})
app.listen(5000, function(){
console.log('Server Listening on', 5000);
})
我的post.js:
var db = require('../db');
var Post = db.model('Post', {
username:{type:String, required:true},
body:{type:String,required:true},
date:{type:Date, required:true,default:Date.now}
});
module.exports = Post;
我的db.js:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/social', function(){
console.log( 'mongoose connected' ); //i am getting consoled
});
module.exports = mongoose;
我对邮递员的尝试:
< a href = https://i.stack.imgur.com/yY9IC.jpg rel = nofollow noreferrer>
有人在这里帮我吗?
推荐答案
您的 res.json(201,post);
是错误的。
如果您要发送状态201,则使用
If you want to send status 201 then use
res.status(201).json(post);
请参见 http://expressjs.com/en/api.html#res.json
这篇关于Express应用程式-无法使用Postman测试Post请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文