GGMU
GGMU

Reputation: 175

displaying html form from a route in node js

I am trying to display the following form in a route in node

<form action="/users/add" method="get">
  First Name:<br> <input type="text" name="first"><br><br>
  Last Name:<br> <input type="text" name="last"><br><br>
  <input type="submit" value="Add User">
</form><br><br>

I my app.js I have a route to handle this,

var express = require('express');
var db = require('db');
var app = express();

// A route to list all users and provide a form to add more.
app.get('/users', function (req, res) {
  res.send('Not working');
});

// Start the server:
var server = app.listen(3000, function () {
  console.log('Listening on port %d', server.address().port);
});

I tried sending the form through the send method but I when I try to run my app I get an error like this:

res.send('<form action="/users/add" method="get">

SyntaxError: Unexpected token ILLEGAL

I think it has to do with the fact I am trying to send html through send with all of the quotes and it just won't work. I looked at the .render express method but that is serving up a static file and that isn't what I want. I need to form to eventually talk to a database.

I am pretty new to node so I am just trying to pick things as I go along.

Upvotes: 0

Views: 792

Answers (1)

Miguel
Miguel

Reputation: 20633

I'm assuming it's because of the line breaks. If that's the case, wrap each line in single quotes and concat with plus sign:

   res.send('<form action="/users/add" method="get">' +
            'First Name:<br> <input type="text" name="first"><br><br>'+
            'Last Name:<br> <input type="text" name="last"><br><br>'+
            '<input type="submit" value="Add User"> '+
            '</form><br><br>');

Upvotes: 1

Related Questions