Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

241
Vistas
How can I connect to POST method in index.js?

Tried to implement a register template with nodejs, console logs the error "POST is not defined".

Here 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>

here the 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')
})

My Server is up on localhost and running, the form is working as well, but it seems as if the POST method can not be called. I didn't connect any database just yet. I'm running on Windows 10. Can somebody tell me why it isn't working?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda