0

I apologize if this has been asked before, if it has please send me in that direction.

I have a node.js app using express. I want to click on a button (form input submit) on the client side and have it run a query on the server side. I am not looking for the query to post or put, I want it to execute a Neo4j query that generates a csv file. The query is not the issue, the query works, the issue is getting the click of a button (form input submit) on client side to run the query on the server side. Here is what I've tried that does not work...

index.ejs

<form method="post" action="/barrelsankeycsv">
    <input class="button" type="submit" value="RunQueryGenCSV">
</form>

...

server.js

app.post('/barrelsankeycsv', async (req, res) => {
  console.log(req.body);
  try {
    const result = await session.run('run neo4j query that generates csv file');
    if (result) {
      res.redirect('/');
      session.close();
    }
  } catch (e) {
    console.log("Something went wrong", e);
  };
});

What am I doing wrong? I've also tried app.all but that doesn't work either. If there is a better way to go about this, I'm all ears.

Many thanks for any help!

2
  • Can you show server console log? Commented Mar 22, 2020 at 7:06
  • You mean to show any errors logged? If so, it's not logging any errors to show. Commented Mar 22, 2020 at 16:02

1 Answer 1

1
  1. Use cURL or Postman to send a POST request to express server. If that works, the problem is on frontend. But I think index.ejs has no problem from what I see.

  2. Add the following code before routing:

app.use(express.urlencoded({ extended: true }))
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @bravemaster, I am successfully sending POST GET PUT DELETE from other parts of my app (index.ejs to server.js), and I added the ...express.urlencoded... as you suggested, but it's still not working.
Thank you again @bravemaster, in the end the error was mine. I had the <form> code seen above nested inside another <form> and that prohibited any action. So in the end I'm marking your comment as 'Helpful' as you confirmed there appeared to be nothing wrong with my code.
@Redlink Thanks. Happy to help :)

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.