0

I am trying to create a simple form handler using express. Here is a similar question but I can't related it with my problem. I have written all of my file Here anyone can check it by using all of my file. For better understanding I copy my code here:
app.js code:

const express = require('express');
const app = express();

//routes
app.get('/',function(req,res){
    res.send('This is root dir');
});

app.get('/friends',function(req,res){
    var friends=['sakib','emon','rayhan'];
    res.render('friends.ejs',{friends:friends});
});

app.get('/addfriend',function(req,res){
    res.send('ADD friend page launch!!!!!');
});

app.listen(3000,function(){
    console.log("server has started on port 3000!!!");
});

ejs(friends.ejs) code:

<h1>Here is list of your Friend:</h1>

<%friends.forEach(function(friend){%>
<li><%=friend%></li>
<%});%>

<form action="/addfriend" method="POST">
    <input type="text" name="friend" placeholder="name">
    <button>
        Submit
    </button>
</form>

When I type any name in the input box and clicked Submit it didn't post to /addfriend. I didn't understand where is my problem. Please goto input box(link) after started the server.

For better understand what's my actual problem is then please use goorm IDE(shared file) where I uploaded everything.

emonhossain
  • 378
  • 2
  • 9

1 Answers1

0

You are using get request instead of post for addfriend.

app.post('/addfriend',function(req,res){
    console.log("I'm called wohoo!");
    res.send('ADD friend page launch!!!!!');
});
Emil
  • 1,186
  • 1
  • 8
  • 17