nodejs not listening to post requests and returns error 505

Viewed 62

i'm just a begineer to nodejs and I am trying to complete this project and i have been stuck since almost 2 days and i'm unable to figure this out. it seems nodejs is not listening to POST requests, I am console logging but it instead returns 405 error page.

Here's the code:

import express, { urlencoded } from 'express';
import path, {dirname} from 'path';
import { fileURLToPath } from 'url';
import mysql from 'mysql';

const app = express();
app.use(express.json());
app.use(urlencoded({ extended : true}));
app.use(express.static('public'));

// const __dirname = path.dirname(fileURLToPath(import.meta.url));
app.post('/' , (req, res)=>{
   console.log(req.body);
//    res.sendFile(path.join(__dirname ,'/public/index.html'));
});

app.listen(5501, ()=>{
    console.log('Server is running at port 5501')
})
<form action="/" method="post" id="cont-form">
        <h1  class="contact-h1">Contact us</h1>
        <div>
            <input type="text" name="name" placeholder="Your name" autocomplete="off">
        </div>
        <div>
            <input type="text" name="subject" placeholder="Subject" autocomplete="off">
        </div>
        <div>
            <input type="email" name="email" placeholder="Email" autocomplete="off">
        </div>
        <div>
          <label for="name">
            <textarea name="message" placeholder="Type your message" cols="30" rows="10"></textarea>
          </label>
        </div>
        <input type="submit" name="submit" id="submit-ms" value="Send message">
      </form>
     <div class="buy">
      <div class="quantity">
      <i class="fa-solid fa-xmark close-buy"></i>
      Select Quantity
      </div>
      <form action='/buy' method="post" >
        <div class="inputs">
        <div class="buy-input-div" style="margin-top:16px">
        <box-icon name='user'></box-icon>
        <input type="text" class="buy-input" name="title" id="title-text"></div>
      <div class="buy-input-div" >
        <box-icon name='user'></box-icon>
        <input type="text" class="buy-input" placeholder="Full Name" name="fullname" id="person-name"></div>
      <div class="buy-input-div">
        <box-icon name='phone'></box-icon>
        <input type="text" class="buy-input" placeholder="Phone Number" name="phone" id="phone"></div>
      <div class="buy-input-div">
        <box-icon name='envelope'></box-icon>
        <input type="text" class="buy-input" placeholder="Email" name="email" id=""></div>
      <div class="buy-input-div">
        <box-icon name='building'></box-icon>
        <input type="text" class="buy-input" placeholder="Address" name="address" id=""></div>
      <div class="buy-input-div">
        <box-icon name='plus'></box-icon>
        <input type="number" class="buy-input" placeholder="Quantity" name="quantity" id="">
      </div>
      <button class="buy-btn">Buy</button>
    </div>
  </form>
  </div>

my file structure:

--root
  --node_modules
  --public
    --index.html
    --AdminPanel.html
    --styles.css
    --script.js
  --index.js
  --package-lock.json
  --package.json

This is how it looks like: This is how it looks like:

enter image description here

1 Answers

You've got only one endpoint using POST method. Browsers sends only GET requests. (405 means Method not allowed).

You need to define a GET endpoint which will serve the html code. And in browser you have to access that endpoint. When you'll press the submit from you server html it will send a POST request.

Edited: If you access that file statically (opening the html file in a browser) then you need to specify in action property of form tag the full address, since / will simply try the location on your machine

Related