1、rest client
一个很好用的rest 接口测试包
(1)使用
建立一个以.http结尾的文件
@uri=http://localhost:5000/product/
GET {{uri}}findAll
###
Get {{uri}}findById/5f4f47de64b53939dc2e2360
###
POST {{uri}}save
Content-Type: application/json
{
"title":"冰淇淋",
"sale":90
}
2、express项目跨域
cors包
使用 app.use(require('cors')())
3、express mongoosedb 链接
var url = process.env.DATABASE_URL || "mongodb://localhost:27017/test";
mongoose.connect(url, {
useNewUrlParser: true
})
.then(() => console.log("Connection Successful"))
.catch(err => console.log(err));
4、mongoosedb 增删改查
app.get("/product/findAll", async (req, res) => {
const data = await Product.find()
res.send(data)
})
app.get("/product/findById/:id", async (req, res) => {
const data = await Product.findById(req.params.id)
res.send(data);
})
app.post("/product/save", async (req, res) => {
const data=req.body;
const product=await Product.create(data);
res.send(product)
})
app.put("/product/update/:id",async(req,res)=>{
const product=await Product.findById(req.params.id);
product.title=req.body.title;
product.sale=req.body.sale;
await product.save();
res.send(product);
})
app.delete("/product/deleteById/:id", async (req,res)=>{
const product=await Product.findById(req.params.id);
await product.remove();
res.send({
success:true
})
})