We are a small company with two system administrators with little knowledge in programming, so if there are idiot mistakes, please forgive in advance.
We are trying to create an application in Javascript+Node.js. We have the application in Azure App Service, and with a local database. We are trying, in parts, to try to get information into the database. In local everything worked perfectly, but when we deploy to the app service, when we try to test, it gives the following message. (Sorry, some files are in spanish)
pruebas.js:6 POST https://ecoalfinventario.azurewebsites.net/crear-ficha 404 (Not Found) crearFicha @ pruebas.js:6 onclick @ (index):27 pruebas.js:19 Error al crear la ficha: SyntaxError: Unexpected token 'T', "The resour"... is not valid JSON
This is the code for the pruebas.js file
function crearFicha() {
const name = document.getElementById("name").value;
const email = document.getElementById("email").value;
const newUser = { name: name, email: email };
fetch('https://ecoalfinventario.azurewebsites.net/crear-ficha', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(newUser)
})
.then(response => response.json())
.then(data => {
console.log("Usuario insertado correctamente en la base de datos");
resetFields();
})
.catch(error => {
console.error("Error al crear la ficha:", error);
});
}
function resetFields() {
document.getElementById("name").value = "";
document.getElementById("email").value = "";
}
our server.js file have
app.use(express.static('https://github.com/Ecoalf/desarrollo'));
app.use(express.urlencoded({extended:false}));
app.use(express.json());
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html'); // Cambia el nombre del archivo HTML si es necesario
});
app.get('/pruebas.js', (req, res) => {
res.setHeader('Content-Type', 'text/javascript'); // Establece el tipo MIME adecuado
res.sendFile(__dirname + '/pruebas.js');
});
// Crear ficha
app.post('/crear-ficha', (req, res) => {
const name = req.body.name;
const email = req.body.email;
connection.query(
'INSERT INTO ecoalf.usuarios (Nombre, Correo) VALUES (?, ?)',
[name, email],
(err, results) => {
if (err) {
console.error("Error al insertar el usuario en la base de datos:", err);
res.status(500).json({ error: "Error al insertar el usuario" });
return;
}
console.log("Usuario insertado correctamente en la base de datos");
res.json({ message: "Usuario insertado correctamente" });
}
);
});
Thank you in advance!