How to send information from an html form to a route? - NodeJS

0

I am a beginner in NodeJS and am responsible for a project in this tool. I'm using the "EJS" engine to create the views. I created a form, to send the information entered by the user, to a route and on this route I process the information and return a response to that page html / ejs, but I do not know how to proceed with the implementation. Follow the code below the html / ejs page and the file code for the routes of my application.

  

artwork_1.ejs

<html>
  <head>
    <title>Página de Conversação</title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
    <h3><%= artwork %></h3>
    <img src="images/IMG01.jpg" alt="Imagem da Obra de Arte 1" title="Obra de Arte 1"></br>

    <div id="divChat">

    </div>

    <form>
        <input type="text" name="question" placeholder="Pergunte-me" required />
        <button>Enviar</button>
    </form>

  </body>
</html>
  

index.js

const express = require('express');
const router = express.Router();


/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: "Página Inicial" });
});

router.get('/artwork-1', function(req, res){
  res.render('artwork_1', { artwork: "Obra 1" });
});

module.exports = router;
    
asked by anonymous 08.10.2017 / 06:30

1 answer

-4
  

Are you using the right expression? use the command npm install -g express -generator, is a module that allows you to create a basic structure for the project. Regarding the code, I have refactored the code that answered you because it did not do what you needed, Now the message is sent and returns a message. The html form:

<form method="post" action="/" >
    <input type="text" name="question" placeholder="Pergunte-me" required />
    <input type="submit" value="Enviar">
</form>
  

add these lines to app.js to return a confirmation message to the site after sending the message.

app.post ('/', function (req, res) {   res.send ('Sent');   });

  

no index.js needed to be made.

    
14.11.2018 / 01:10