Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Node.JS How do I collect HTML form data when hosting using Node.js as backend?

eddystark

New Coder
I am trying to setup a small application to collect form data from users and store it on MongoDB database. I was able to do this on my local server by setting the HTML action attribute to point to my localhost route used for setting up the Express post method which is http://localhost:4000/login. I also connected to the MongoDB database using mongodb://localhost:27017/db and everything works fine. My question is how do I make this work on a hosting platform like Netlify which I am currently using? Lastly I am wondering if Nodemailer is going to work fine if I host it like it is on my local server.
My code looks like this:

HTML:

HTML:
<form action="http://localhost:4000/login" method="post">
   <input type="text" placeholder="Email address or phone number" name="email"/>
   <input type="password" placeholder="Password" name="password"/>
   <input type="submit" value="Log In"/>
</form>

NODE.JS:

JavaScript:
mongoose.connect('mongodb://localhost:27017/db')

app.use(express.json())
app.use(express.urlencoded({extended: false}))

app.post('/login', async (req, res) => {
    const data = req.body
    const response = await Login.create(data)
})
 
I am trying to setup a small application to collect form data from users and store it on MongoDB database. I was able to do this on my local server by setting the HTML action attribute to point to my localhost route used for setting up the Express post method which is http://localhost:4000/login. I also connected to the MongoDB database using mongodb://localhost:27017/db and everything works fine. My question is how do I make this work on a hosting platform like Netlify which I am currently using? Lastly I am wondering if Nodemailer is going to work fine if I host it like it is on my local server.
My code looks like this:

HTML:

HTML:
<form action="http://localhost:4000/login" method="post">
   <input type="text" placeholder="Email address or phone number" name="email"/>
   <input type="password" placeholder="Password" name="password"/>
   <input type="submit" value="Log In"/>
</form>

NODE.JS:

JavaScript:
mongoose.connect('mongodb://localhost:27017/db')

app.use(express.json())
app.use(express.urlencoded({extended: false}))

app.post('/login', async (req, res) => {
    const data = req.body
    const response = await Login.create(data)
})
your code should work just fine. Rather than using absolute paths, use relative paths. Also, I would recommend using nodemailer with the smtp credentials of your email provider. Although nodemailer does allow you to send off an email without smtp, these emails will get flagged as spam by your email provider. Authenticating the smtp should help
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom