Intenté implementar una plantilla de registro con nodejs, la consola registra el error "POST no está definido".
Aquí index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <meta name="viewpost" content="width=device-width, initial-scale=1.0"/> <title>Login & Register with NodeJS</title> </head> <body> <form id="reg-form"> <input type="text" autocomplete="off" placeholder="Username" id="username"> <input type="password" placeholder="Password" id="password"> <input type="submit" value="Submit Form"> </form> <script> const form = document.getElementById('reg-form') form.addEventListener('submit', registerUser) async function registerUser(event) { event.preventDefault() try { const username = document.getElementById('username').value const password = document.getElementById('password').value } catch(e) { console.log( "Assignment didn't work " + e.message) } try { const result = await fetch('./api/register', { method: POST, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password }).then((res) => res.json()) }) console.log(result) } catch(e) { console.log(e.message) } } </script> </body> </html>aquí el index.js:
const express = require('express') const path = require('path') const app = express() app.use('/', express.static(path.join(__dirname, 'static'))) app.use(express.json()) app.post('./api/register', async (req, res) => { console.log(req.body) res.json({status: 'ok'}) }) app.listen(9999, () => { console.log('Server up at 9999') })Mi servidor está en localhost y en ejecución, el formulario también funciona, pero parece que no se puede llamar al método POST. Todavía no conecté ninguna base de datos. Estoy ejecutando Windows 10. ¿Alguien puede decirme por qué no funciona?