Node.js export.create和router.post有什么区别?
我见过两种创建restful API的方法 1:“export.create”,一旦在postman上添加代码,就必须以json格式添加代码 2:“router.post”,我知道它使用的是express,当您在postman上添加代码时,您可以使用“x-www-form-urlencoded”添加它 有什么区别Node.js export.create和router.post有什么区别?,node.js,express,sequelize.js,postman,Node.js,Express,Sequelize.js,Postman,我见过两种创建restful API的方法 1:“export.create”,一旦在postman上添加代码,就必须以json格式添加代码 2:“router.post”,我知道它使用的是express,当您在postman上添加代码时,您可以使用“x-www-form-urlencoded”添加它 有什么区别 router.post("/", (req, res) => { if(!req.body.certifications, !req.body.member
router.post("/", (req, res) => {
if(!req.body.certifications,
!req.body.memberships,
!req.body.hobbies,
!req.body.interests) {
res.status(400)
res.json({
error: "Bad Data"
})
} else {
Basic.create(req.body)
.then(() => {
res.send("Basic Added")
})
.catch(err => {
res.send("Error: " + err)
})
}
})
------------------------------------------
exports.create = (req, res) => {
var customer;
Customer.create({
firstname: req.body.firstname,
lastname: req.body.lastname,
age: req.body.age
}).then(createdCustomer => {
// Send Created Customer to client
customer = createdCustomer;
return Address.create({
street: req.body.street,
phone: req.body.phone
})
}).then(address => {
customer.setAddress(address)
res.send('OK');
})
};
exports.create
仅将方法作为模块的一部分导出,然后可与另一个文件中的router.post
一起使用
大概是这样的:
const { create } = require('./the_file_name.js');
router.post('/', create);
有道理。你知道为什么人们使用不同类型的格式(JSON)(x-www-form-urlencoded)添加数据吗?这可能是一个完全不同的问题。老实说,这完全取决于你希望你的客户给你寄什么。不同的类型有不同的优点/缺点。如果您对性能/数据大小/客户端限制没有任何特定要求,那么JSON似乎是目前的标准默认值。易于实现,易于阅读,广泛支持,足够大的尺寸。