0

A very simple page... server.js

var path = require("path");
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
app.listen(8000, function() {
  console.log("listening on port 8000");
})
app.use(bodyParser.urlencoded());
app.use(express.static(path.join(__dirname, "./static")));
app.set('views', path.join(__dirname, './views'));
app.set('view engine', 'ejs');
app.get('/', function(req, res) {
 res.render('index');
})
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/basic_mongoose');
var QuoteSchema = new mongoose.Schema({
    name: String,
    quote: String
})
var Quote = mongoose.model('Quote', QuoteSchema);

app.post('/quotes', function(req, res) {
  console.log("POST DATA", req.body);
  var quote = new Quote({name: req.body.name, quote: req.body.quote});

  quote.save(function(err) {
        if(err) {
          console.log('something went wrong');
        } else { 
          console.log('successfully added a quote!');
        res.redirect('/main');
        }
  })
})

index.ejs

<html>
<head>
<title></title>
</head>
<body>
    <div id='container'>
       <h2>Welcome to Quoting Dojo</h2>
       <form action='/quotes' method='post'>
            <p>Your Name: <input type = 'text' id ='name'/><p>
            <p>Your Quote: <input type='text' id ='quote'/><p>
            <button id='add' type='submit'>Add Quote</button>
        </form>
       <button id='skip'>Skip to </button>
    </div>    
</body>
</html>

I get "cannot /POST" when I click the submit button. Anyone? Server loads, html page in chrome shows up. I fill in the boxes and click "add". I get the route http://localhost:8000/quotes and the error Cannot POST /quotes. Not sure what I did as I have action='post' to /quotes and app.post in the server. I don't know if I have any other issues. Thanks.

Dave Newton
  • 152,765
  • 23
  • 240
  • 286
SadieRose
  • 79
  • 8

2 Answers2

0

Use the name attribute instead of id, name is sent to the server, id is not.

HTML input - name vs. id

Community
  • 1
  • 1
user2182349
  • 8,572
  • 3
  • 22
  • 36
0
<form action='/quotes' method='post'>
            <p>Your Name: <input name='name' type = 'text' id ='name'/><p>
            <p>Your Quote: <input name='quote' type='text' id ='quote'/><p>
            <button id='add' type='submit'>Add Quote</button>
        </form>
HDK
  • 808
  • 1
  • 8
  • 13