相关文章推荐
风流倜傥的蛋挞  ·  [6]elasticsearch源码深入分析 ...·  2 年前    · 
文武双全的圣诞树  ·  Powershell ...·  2 年前    · 
愉快的黄豆  ·  C++实现string存取二进制数据的方法_ ...·  2 年前    · 
豁达的可乐  ·  一个JavaScript贷款计算器 - 知乎·  2 年前    · 
想表白的烤土司  ·  java - Difference ...·  2 年前    · 
小百科  ›  尝试发送POST请求时收到404错误开发者社区
软件 post请求 const 404错误
完美的青蛙
1 年前
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
提问

问 尝试发送POST请求时收到404错误

Stack Overflow用户
提问于 2018-12-17 04:44:23
EN

我正在尝试使用node.js设置一个API,并在我的app.js类中处理请求错误,其中我返回一个404以防出现错误,现在这就是我的问题,我看不出我的请求有什么错误,我仍然收到404错误,我试图向我的API发送一个post请求,就像这样:

{
    "name":"Harry Potter 5",
    "price":"12.99"
}

然后我得到了这个

​

​

这是我的app.js

const express = require('express');
const app = express();
const morgan = require('morgan');
const productRoutes = require('./api/routes/product');
const orderRoutes = require('./api/routes/order');
const bodyParser = require('body-parser');
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({
    extended:false
app.use(bodyParser.json());
app.use((req, res, next) => {
    const error = new Error("Not found");
    error.status = 404;
    next(error);
  app.use((error, req, res, next) => {
    res.status(error.status || 500);
    res.json({
      error: {
        message: error.message
app.use('/products', productRoutes);
app.use('/orders', orderRoutes);
module.exports = app;

这是我的product.js

const express = require('express');
const router = express.Router();
router.get('/', (req, res, next) => {
    res.status(200).json({
        message: 'Handling GET requests to /products'
router.post('/', (req, res, next) => {
    const product = {
        name: req.body.name,
        price: req.body.price
    res.status(201).json({
        message: 'Handling POST requests to /products',
        createdProduct: product
router.get('/:productId', (req, res, next) => {
    const id = req.params.productId;
    if (id === 'special') {
        res.status(200).json({
            message: 'You discovered the special ID',
            id: id
    } else {
        res.status(200).json({
            message: 'You passed an ID'
router.patch('/:productId', (req, res, next) => {
    res.status(200).json({
        message: 'Updated product!'
router.delete('/:productId', (req, res, next) => {
    res.status(200).json({
        message: 'Deleted product!'
module.exports = router;
1 59 0 票数 0
EN
javascript
node.js
api
express

回答 1

Stack Overflow用户

发布于 2018-12-17 05:03:15

这是因为任何请求都会执行404处理程序。

看看你的代码的这个精简版本:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
    extended:false
app.use(bodyParser.json());
app.use((req, res, next) => {
  console.log("Got into 404 handler");
  const error = new Error("Not found");
  error.status = 404;
  next(error);
app.use((error, req, res, next) => {
  console.log("Got into 500 handler");
  res.status(error.status || 500);
  res.json({
    error: {
      message: error.message
app.use('/products', (req, res, next) => {
  console.log("Got into 200 handler");
  res.status(200).end();
app.listen(8080);

它在每次请求时输出“进入404处理程序”。现在,如果这样注释掉404回调:所有请求都会经过500和200回调:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
    extended:false
app.use(bodyParser.json());
/* there used to be the 404 callback here */
app.use((error, req, res, next) => {
  console.log("Got into 500 handler");
  res.status(error.status || 500);
  res.json({
    error: {
      message: error.message
app.use('/products', (req, res, next) => {
  console.log("Got into 200 handler");
  res.status(200).end();
app.listen(8080);

现在,在您的特定问题中,下面的代码可以工作(我只是交换了处理程序的顺序):

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
    extended:false
app.use(bodyParser.json());
app.use('/products', (req, res, next) => {
  console.log("Got into 200 handler");
  res.status(200).end();
app.use((req, res, next) => {
  console.log("Got into 404 handler");
  const error = new Error("Not found");
  error.status = 404;
  next(error);
app.use((error, req, res, next) => {
  console.log("Got into 500 handler");
  res.status(error.status || 500);
  res.json({
    error: {
      message: error.message
 
推荐文章
风流倜傥的蛋挞  ·  [6]elasticsearch源码深入分析——API源码分析 - 简书
2 年前
文武双全的圣诞树  ·  Powershell 获取当前运行服务_专注挖煤二十年的博客-CSDN博客
2 年前
愉快的黄豆  ·  C++实现string存取二进制数据的方法_光阴迷客的博客-CSDN博客
2 年前
豁达的可乐  ·  一个JavaScript贷款计算器 - 知乎
2 年前
想表白的烤土司  ·  java - Difference between getClass().getClassLoader().getResource() and getClass.getResource()? - Stack Overflow
2 年前
Link管理   ·   Sov5搜索   ·   小百科
小百科 - 百科知识指南