POST http://localhost:3000/ 404 (Not Found) Не могу добавить информацию в БД

index.js

const express = require('express');
const mongoose  = require('mongoose');
const Restaurant = require('./models/Restaurant');
const multer = require('multer');
const cors = require('cors');


const app = express();
const uploadMiddleware = multer();

app.use(cors({origin:'http://localhost:3000'}));
app.use(express.json());

mongoose
.connect('mongodb+srv://admin:[email protected]/?retryWrites=true&w=majority&appName=Cluster0')
.then(() => console.log('DB connected'))
.catch((err) => {
    console.error(err);
    process.exit(1);
});

app.get('/', (req, res) => {
    console.log('Это функция промежуточного программного обеспечения');
});

app.post('/',uploadMiddleware, async (req, res) => {

    const {title, description, foodType, location} = req.body;
    const postDoc = await Restaurant.create({
        title, 
        description, 
        foodType,
        location,
    })

    res.json({postDoc});
})

app.listen(3000);

CreateRestaurantPage.js

import { useState } from "react";

export default function CreateRestaurantPage() {

    const [title, setTitle] = useState('');
    const [description, setDescription]  = useState('');
    const [foodType, setFoodType] = useState('');
    const [location, setLocation] = useState('');

    async function createNewRestaurant(ev) {
        const data = new FormData();
        data.set('title', title);
        data.set('description', description);
        data.set('foodType', foodType);
        data.set('location', location )

        ev.preventDefault();
        const response = await fetch('http://localhost:3000', {
            method: 'POST',
            body: data,
        });
        if (response.ok) {
            console.log("data sent")
        }
    }

    return (
        <form onSubmit={createNewRestaurant}>
            <input 
                type="title" 
                placeholder={"Название"} 
                value={title}
                onChange={ev => setTitle(ev.target.value)}/>
            <input 
                type="description" 
                placeholder={"Описание"}
                value={description}
                onChange={ev => setDescription(ev.target.value)}/>   
            <input 
                type="foodType" 
                placeholder={"Кухня"}
                value={foodType}
                onChange={ev => setFoodType(ev.target.value)}/> 
            <input 
                type="location" 
                placeholder={"Локация"}
                value={location}
                onChange={ev => setLocation(ev.target.value)}/>   
            <button style={{marginTop: '5px'}}>Добавить ресторан</button>
        </form>
    )
}

Restaurant.js

const mongoose = require('mongoose');
const {Schema, model} = mongoose;

const RestaurantSchema = new Schema({
    title: String,
    description: String,
    foodType: String,  
    location: String,
}
);

const RestaurantModel = model('Restaurant', RestaurantSchema);
module.exports = RestaurantModel;

Ответы (0 шт):