express - Does the route of a .post() method matter?
问题描述
I get the same behavior with all of the following code examples even though they have different routes in the .post() method. Is there a case where the route would matter?
Example 1:
index.js
app.post("/", async (req, res) => {
const newProduct = new Product(req.body);
await newProduct.save();
res.redirect(`/products/${newProduct._id}`);
});
index.ejs
<form action="/" method="post">
Example 2:
index.js
app.post("/products", async (req, res) => {
const newProduct = new Product(req.body);
await newProduct.save();
res.redirect(`/products/${newProduct._id}`);
});
index.ejs
<form action="/products" method="post">
解决方案
The resource you're sending your POST request to would matter in most APIs, particularly REST APIs. What difference it makes is up to your particular API specification.
The resource generally specifies what type of resource you're trying to manipulate, so if you send a POST request to "/users", you're trying to create/update a user. If you send a request to e.g. "/products", you're trying to create/update a product. Business logic and validation rules would be different for the different resources.
Inside your specific API you're probably free to do whatever you like, you can create an endpoint with the resource name "/unicorns" that returns pictures of airplanes, but your intention will not be very clear to other developers. If you handle different types of resources, you probably want to register them to different resource endpoints in order to encode the intention of the request.
推荐阅读
- python - 线程化 Python3
- javascript - TypeError:无法读取 ElectronJS 应用程序中未定义的属性“协议”
- flutter - 如何在颤振上居中图像
- sas - 如何创建名称与两个数组变量名称连接的变量
- ckeditor - TYPO3 CKE 编辑器允许数据属性
- amazon-web-services - 如何使用来自目的地的 SNS 中的属性过滤 Lambda 响应?
- go - 与官方 Docker 镜像相比,Go Mime 包(1.14)在本地表现不同
- android - 在颤动中获取电话号码
- php - 包含指向 sprintf 图像的链接
- laravel - 第一次运行 Laravel 时出错