Redink
Redink

Reputation: 344

Execute Query on Node.js Using Express and Form Input Submit

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!

Upvotes: 0

Views: 586

Answers (1)

glinda93
glinda93

Reputation: 8479

  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 }))

Upvotes: 1

Related Questions