Код: Выделить всё
POST http://localhost:3000/auth/save-user 404 (Not Found)
submitForm @ signin.html:88
onclick @ signin.html:58
Html-часть:
Код: Выделить всё
// Send user data to backend using fetch
fetch('http://localhost:3000/auth/save-user', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(userData),
})
Код: Выделить всё
const express = require('express');
const router = express.Router();
const fs = require('fs');
const path = require('path');
// Path to users.json
const usersFilePath = path.join(__dirname, 'users.json');
// POST route to save a user
router.post('/save-user', (req, res) => {
const userData = req.body;
// Validate input data
if (!userData.name || !userData.email || !userData.password) {
return res.status(400).json({ message: 'All fields are required' });
}
// Read the users file
fs.readFile(usersFilePath, 'utf8', (err, data) => {
if (err) {
return res.status(500).json({ message: 'Error reading users file' });
}
const users = JSON.parse(data || '[]'); // Parse existing users or initialize as empty array
// Check if user already exists
const userExists = users.some(user => user.email === userData.email);
if (userExists) {
return res.status(400).json({ message: 'User already exists' });
}
// Add the new user to the list
users.push(userData);
// Write updated data back to users.json
fs.writeFile(usersFilePath, JSON.stringify(users, null, 2), (err) => {
if (err) {
return res.status(500).json({ message: 'Error saving user' });
}
// Respond with success
res.status(200).json({ message: 'User saved successfully!' });
});
});
});
// Export router
module.exports = router;
может ли кто-нибудь с этим помочь?
Подробнее здесь: https://stackoverflow.com/questions/791 ... -not-found