ExpressJS Select Options


I have a form that has a select element with options. Think states. How do I ‘select’ the value of the state that’s in the database on my edit route?

This user has “state” : “TX”, in the database.

One option would be:

app.patch('/:state'), (req, res) => {
  const state = req.params.state;

Now if you make PATCH request to http://your_server/TX you’ll have state variable set to TX.

Another option would be to send data in request body, use body-parser middleware and then access your data in req.body.


Thanks for the reply. <%= console.log(user.state) %> does return the state. On the edit.ejs how do I tell the select, choose those value from user.state?

<select class="form-control" name="user[state]" id="state">
    <option value="">Please Select</option>
    <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>
    <option value="AZ">Arizona</option>
    <option value="AR">Arkansas</option>
    <option value="CA">California</option>