1

This question title was edited. The original title was "How can I submit a string in a form and have it applied as a request parameter in the url?".


Think of a simple server where a string is received as a route parameter, e.g. with localhost:3000/hello it serves a page containing 'hello'.

This is easily implemented, here is a simple express server doing that:

var express = require("express");
var app = express();

app.get("/:string", function(req, res) {
  res.send(req.params.string);
});

app.listen(3000);

Is it possible to supply the string via a form?

stefaleon
  • 91
  • 1
  • 10

1 Answers1

0
  • Assigning a value to req.params via an html form is not inherently feasible. Forms assign values easily to req.query instead.

  • Definitions for req,params and req.query described in Express 4.x API Reference:

req.params
This property is an object containing properties mapped to the named route “parameters”. For example, if you have the route /user/:name, then the “name” property is available as req.params.name. This object defaults to {}.

req.query
This property is an object containing a property for each query string parameter in the route. If there is no query string, it is the empty object, {}.

  • Arjan's answer in this question cites hidden type inputs which could be helpful in certain use cases.
Community
  • 1
  • 1
stefaleon
  • 91
  • 1
  • 10