Я копировал этот код из видео на YouTube о том, как сделать API отдыха с помощью Node.js. Запрос GET
и другие вещи работали нормально, пока я не попробовал запрос POST
, затем я обнаружил ошибку:
Could not get any response
There was an error connecting to http://localhost:3000/posts.
Why this might have happened:
The server couldn't send a response:
Ensure that the backend is working properly
Self-signed SSL certificates are being blocked:
Fix this by turning off 'SSL certificate verification' in Settings > General
Proxy configured incorrectly
Ensure that proxy is configured correctly in Settings > Proxy
Request timeout:
Change request timeout in Settings > General
, и я проверил консоль, и вот что она показала
POST http://localhost:3000/posts
Error: socket hang up
Warning: This request did not get sent completely and might not have all the required system headers
Request Headers
Content-Type: application/json
User-Agent: PostmanRuntime/7.24.0
Accept: */*
Cache-Control: no-cache
Postman-Token: ee8e3956-6963-45a5-8c7e-3bd41628b210
Host: localhost:3000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Request Body
Человек на видео отлично работал на него, поэтому я думаю, что, возможно, это как-то связано с новыми обновлениями, поскольку видео немного устарело. Моя версия Postman - 7.21.2
Я был бы признателен, если бы кто-нибудь помог решить эту проблему, так как я искал в Интернете различные ответы, и я не могу найти одну конкретную c на свой
Ниже приведено приложение. js где я установил порт и маршрутизатор
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
require("dotenv/config");
app.use(bodyParser.json());
// Import Routes
const postsRoute = require("./routes/posts");
//Middlewares
app.use("/posts", postsRoute);
//Routes
app.get("/", (req, res) => {
res.send("we are on home");
});
//Connect to DB
mongoose.connect(
process.env.DB_CONNECT,
{ useNewUrlParser: true, useUnifiedTopology: true },
() => console.log("connected to DB!")
);
app.listen(3000);
, а вот код сообщения. js маршрут
const express = require("express");
const router = express.Router();
const Post = require("../models/Post");
router.get("/", (req, res) => {
res.send("we are on posts");
});
router.post("/", async (req, res) => {
const post = new Post({
title: req.body.title,
description: req.body.description,
});
try {
const savedPost = await post.save();
res.json(savedPost);
} catch (err) {
res.json({ message: err });
}
});
module.exports = router;