Answer the question
In order to leave comments, you need to log in
Why does the request with the PUT method not work?
Upon request
http://localhost:3000/api/tour/1?name=newName&price=199
Cannot 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
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question