Skip to content

Instantly share code, notes, and snippets.

@tjkhara
Last active November 6, 2020 14:25
Show Gist options
  • Select an option

  • Save tjkhara/c6102bbbef99cec1755a688dee6b4846 to your computer and use it in GitHub Desktop.

Select an option

Save tjkhara/c6102bbbef99cec1755a688dee6b4846 to your computer and use it in GitHub Desktop.
Getting started with express
// Import express
let express = require('express')
// Create an instance of it
let ourApp = express()
// Use third party requests library
ourApp.use(express.urlencoded({extended: false}))
// get function for root URL
ourApp.get('/', function(req, res){
res.send(`
<form action="/answer" method="POST">
<p>What color is the sky?</p>
<input name="skyColor" autocomplete="off">
<button>Submit answer</button>
</form>
`)
})
// post of that form
ourApp.post('/answer', function(req, res){
if(req.body.skyColor == 'blue'){
res.send(`
<p>That is correct</p>
<a href="/">Back to home</a>
`)
} else {
res.send(`
<p>That is not correct.</p>
<a href="/">Back to home</a>
`)
}
})
// Get request at /answer
ourApp.get('/answer', function(req, res){
res.send(`<p>Are you lost?</p>`)
})
// Define port
ourApp.listen(3000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment