Приходит html вместо json
Структура проекта
- frontend
- components
- Employee.tsx
- Employees.tsx
- pages
- components
- {... и так далее}
- backend
- index.js
- controllers
- controller.js
- routes
- route.js
- models
- Employee.js
- .env
Содержимое файлов:
//index.js
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const dotenv = require('dotenv');
const employeeRoutes = require('./routes/route');
dotenv.config();
const app = express();
app.use(cors());
app.use(express.json());
mongoose.connect(process.env.MONGODB_URI)
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('Something went wrong', err));
app.use('/api', employeeRoutes);
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
//controllers/controller.js
const Employee = require('../models/Employee');
const getAllEmployees = async (req, res) => {
try {
const employees = await Employee.find();
res.json(employees);
} catch (err) {
res.status(500).json({ message: err.message });
}
};
module.exports = { getAllEmployees}
//routes/route.js
const express = require('express');
const router = express.Router();
const { getAllEmployees, createEmployee } = require('../controllers/controller');
router.get('/getemployees', getAllEmployees);
//router.post('/addemployee', createEmployee);
module.exports = router;
//models/Employee.js
const mongoose = require('mongoose');
const employeeSchema = new mongoose.Schema({
_id: {type: Number, required: true},
firsName: { type: String, required: true },
lastName: { type: String, required: true },
imageUrl: {type: String, required: true},
phone: { type: String, required: true },
email: { type: String, required: true, unique: true },
position: { type: String, required: true },
description: { type: String, required: true },
likes: { type: String, required: true },
});
const Employee = mongoose.model('Employee', employeeSchema);
module.exports = Employee;
//Employees.tsx
const response = await axios.get('http://localhost:3000/api/getemployees');
Проблема:
- Приходит HTML документ вместо json (из-за этого вот такая ошибка employees.map is not a function)
- Еще есть вот такое Could not load content for webpack:///react-devtools-shared/src/backend/console.js (Fetch through target failed: Unsupported URL scheme; Fallback: HTTP error: status code 404, net::ERR_UNKNOWN_URL_SCHEME)
Пожалуйста, помогите понять, что не так.