Last active
November 6, 2020 14:25
-
-
Save tjkhara/c6102bbbef99cec1755a688dee6b4846 to your computer and use it in GitHub Desktop.
Getting started with express
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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