javascript - DELETE api 请求响应 404 错误

标签 javascript node.js mongodb api express

过去 4 小时我一直在绞尽脑汁试图找出为什么我的 DELETE 请求不起作用。它返回 404 未找到响应。我的 POST、PUT 和 GET 都工作正常。

我使用 Chrome 的 Postman 来执行请求,我的 url 格式是:

删除http://localhost:3000/api/products/568c39bfba6030c90c36a061

这是我的代码。

服务器.js:

var express = require('express');


var mongoose = require('mongoose'); 

var bodyParser =require('body-parser');


mongoose.connect('mongodb://127.0.0.1:27017/test');

var app = express(); 
app.use(bodyParser.urlencoded({ extend: true})); 
app.use(bodyParser.json());

app.use('/api', require('./routes/api')); 

app.listen(3000); 
console.log('API is running on port 3000');

API.js:

var express = require('express'); 

var router = express.Router(); 


var Product = require('../models/product');


console.log(Product);


Product.methods(['get', 'post', 'put',' delete']); 

Product.register(router, '/products'); 

module.exports = router;

产品.js

var restful = require('node-restful'); 

var mongoose = restful.mongoose; 



var productSchema = new mongoose.Schema({

    name: String, 
    sku: String, 
    price: Number
});

module.exports = restful.model('Products', productSchema); 

最佳答案

您的删除方法名称中有一个空格('delete')。

关于javascript - DELETE api 请求响应 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35375389/

相关文章:

ssh - 创建了没有 ssh key 的 no.de smartmachine,可以修复吗?

node.js - 没有 Schema.ObjectId 的 Mongoose 中的 DB-ref?

javascript - 我可以在不使用图像元素的情况下将图像直接加载到 Canvas 上吗

javascript - 按钮 onclick 不适用于 Javascript 查询

javascript - 同时使用 window.location 和 return confirm

javascript - 需要 Express 中的东西

node.js - 如何检查网站是否有自定义 404 页面或默认页面?

c# - Mongodb 约定包

javascript - 从 JSON 数据将 CSV 文件上传到 S3 存储桶

javascript - 如何向 JavaScript Date 对象添加年、月、日、小时、分钟或秒?