0

I am trying to create a login page using html and node.js. I have been able to send stuff to the database using only node.js and postman but now I am trying to get it working with my html page. When trying to send stuff, I get sent back to index.html and a scripts.js error. The scripts.js error comes from the index.html page but it only happens when I get redirected. That error is not my problem though because I cant even get my db to update which is the biggest part I am trying to solve.

app.js

app.use('/public', express.static(path.join(__dirname, 'static')));
app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'static', 'index.html'));
});
app.use(bodyParser.json());
const { urlencoded } = require('body-parser');
app.use(express.urlencoded({ extended: true }))

const authRoute = require('./routes/auth');

app.use('/api/user', authRoute);
app.use('/api/posts', postRoute);

I am using a routes folder where I contain the auth.js. auth.js also contains the methods I use to login in and registering a account

auth.js

const User = require('../models/User');
const { registerValidation, loginValidation } = require('../validation');
router.post('/register', async (req, res) => {...}
router.post('/login', async (req, res) => {...}

register.html

        <form action="/" method="/api/user/register">
            <input type="name" placeholder="Name" name="name" id="name" required>
            <input type="text" placeholder="Email" name="email" id="email" required>
            <input type="password" placeholder="Password" name="passwrd" id="password" required>
            <!-- <input type="password2" placeholder="Repeat Password" name="psw-repeat" id="psw-repeat" required> -->
            <!--<button type="submit" class="registerbtn">Register</button>-->
            <input type="submit" value="Continue">
        </form>
    </div>
</div>

Using postman when I need to register a account I do

http://localhost:3000/api/user/register 
{
    "name": "POST MAN",
    "email": "[email protected]",
    "password": "123456"
}

Similar to login except I do /login and only use email and password. If I can get the register part working than I would be able to get the rest working as well.

1 Answer 1

2

Try modifying Form tag’s attributes in html

action=“BE URL or api Path” method=“POST”

Reference: https://www.w3schools.com/tags/att_form_method.asp

Sign up to request clarification or add additional context in comments.

3 Comments

OMG it worked. I also had to remove const { urlencoded } = require('body-parser'); from my app.js. It was causing giving me a cannot POST message.
Now I got to figure out how to redirect user back to the website homepage and display username instead on login.
Res.redirect function try that link: masteringjs.io/tutorials/express/redirect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.