Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!