G
G
gleendo2017-08-22 18:37:04
Node.js
gleendo, 2017-08-22 18:37:04

Why does the request with the PUT method not work?

Upon request

http://localhost:3000/api/tour/1?name=newName&price=199

IssuesCannot GET /api/tour/1
const express = require("express");
const handlebars = require("express-handlebars");

// const fortune = require("./lib/fortune");

const app = express();

const tours = [
  {
    id: 0,
    name: "Река Худ",
    price: 99.99
  },
  {
    id: 1,
    name: "Орегон Коуст",
    price: 149.95
  }
];

app.disable("x-powered-by");

app.engine("handlebars", handlebars({
  defaultLayout: "main"
}));

app.set("view engine", "handlebars");

// app.use(express.static(__dirname + "/public"));

app.put("/api/tour/:id", (req, res) => {
  let tour = tours.find(tour => tour.id === req.params.id);

  if (tour) {
    if (req.query.name) {
      tour.name = req.query.name;
    }

    if (req.query.price) {
      tour.price = req.query.price;
    }

    res.json(tour);
  } else {
    res.json({
      error: "Такого тура не существует."
    });
  }
});

// app.get("/", (req, res) => {
//   res.render("home");
// });

app.listen(3000);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Abcdefgk, 2017-08-22
@Abcdefgk

Probably because it's not an AJAX request (although I can't see the client side and I'm just guessing), which means it's a GET. Besides, you also process it - as GET.

A
abberati, 2017-08-22
@abberati

Because you are making a request with a GET method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question