0

I am new to node.js. I am connecting with an api with express/node.js with ejs templating. I wanted to push some information from the browser to the api. At the moment, I can push from the command line. I understand I cannot call a node.js file from the browser directly but I was wondering when I click submit on a form if it can call node.js file and run it...what should I use, modules, routes, ajax, or any other solutions you recommend? I appreciate the help.

Lily
  • 1
  • 3
  • Did [my answer](https://stackoverflow.com/questions/45241464/running-a-node-js-file-from-a-click-event/45243173#45243173) below help you? Any comments? – rsp Jul 23 '17 at 12:26

2 Answers2

0

Well, it's a strange question. Your Node application needs to either listen for HTTP requests or WebSocket connections (possibly using some abstraction like Socket.io that can provide fallbacks where WebSocket is not available) and handle the requests or messages sent on the socket. In those handlers you can do whatever you need. This is just a basic principle of client/server architecture.

Now, to choose what technology is best suited for your needs it all depends on how often you need to make that communication, whether or not you need realtime communication, how much data do you need to pass around, if you need to support serving data initiated by the server or only by the client etc.

To answer you question - yes, you can submit a form and make it execute some code in your Node application. This is exactly how forms work - they make a connection, usually GET with data in the query string or POST with data in the body (both can easily be read by Node) and then your handler on the backend handles the request, does whatever it needs and sends a response.

Consider this simple example using express and body-parser:

const app = require('express')();
const { urlencoded } = require('body-parser');

app.use(urlencoded({ extended: true }));

app.use('/', (req, res) => {
  const { method, path } = req;
  const { x } = req.body;
  console.log(`Client request: ${method} ${path} (x = ${x})`);
  res.end(`
    <!doctype html>
    <html>
      <head>
        <title>Form handling example</title>
      </head>
      <body>
        <p>x = ${x}</p>
        <form method="POST" action="/">
          Enter x: <input type="text" name="x">
          <input type="submit" value="Submit">
        </form>
      </body>
    </html>
  `);
});

app.listen(4447, () => console.log('Listening on http://localhost:4447/'));

Create a new directory, save this code as server.js, run:

npm init -y
npm install express body-parser -S
node server.js

and access the printed URL in the browser.

When you click the submit button you'll see what will happen in the Node app.

rsp
  • 107,747
  • 29
  • 201
  • 177
0

Your node app will have a route set up which accepts GET or POST requests. Then it does some logic and returns some data in a response.

Your web page will send the form to your node route as GET or POST via an AJAX call, and likewise it will receive a response from the AJAX call. Then you can take this response and use it locally in the webpage however you like.

Bango
  • 971
  • 6
  • 18