1
let express = require("express") 
let ourApp = express()         

ourApp.get("/", function(req, res) {
    res.send(`
    <form action='/answer' method='POST'>
    <p>What color is the sky on a clear and sunny day?</p>
    <input name="skyColor" autocomplete="off">
    <button>Submit Answer</button>
    </form>
    `)
})

ourApp.post('/answer', function(req, res) {
   if(req.body.skyColor == "BLUE") {
    res.send(`
    <p>Congrats! You got the right answer.</p>
    <a href="/">Back to homepage</a>
    `)
    } else  {
        res.send(`
        <p>Sorry, you got the wrond answer. Please try again</p>
        <a href="/">Back to homepage</a>
        `)
    }
})

ourApp.get('/answer', function(req, res) {
    res.send("Are you lost? There is nothing to see here.")
})
ourApp.listen(3000)

It keeps telling me

TypeError: Cannot read property 'skyBlue' of undefined.

I'm new to node and express so any help would be appreciated

  • Welcome to stackOverflow. You might want to search for similar questions before posting. Also, a more descriptive title is helpful, e.g. "cannot access to POST form's req.body with express server" or a variation. – Mehdi Oct 13 '21 at 13:26
  • thanks, new to Stackoverflow. I'm looking at the article now. – Maxwell200073 Oct 13 '21 at 13:41
  • 1
    I figured it out. Thank you Mehdi. I needed to add ourApp.use(express.urlencoded({extended:false})) at the top. – Maxwell200073 Oct 13 '21 at 13:55

0 Answers0