Skip to content

Instantly share code, notes, and snippets.

@rajivnarayana
Created July 1, 2021 11:26
Show Gist options
  • Select an option

  • Save rajivnarayana/05b00f3dd836aa4da20b8ca6240a09ae to your computer and use it in GitHub Desktop.

Select an option

Save rajivnarayana/05b00f3dd836aa4da20b8ca6240a09ae to your computer and use it in GitHub Desktop.

Revisions

  1. rajivnarayana created this gist Jul 1, 2021.
    36 changes: 36 additions & 0 deletions addition-express.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    const express = require("express");
    const bodyParser = require('body-parser');

    const app = express();
    // parse application/x-www-form-urlencoded
    app.use(bodyParser.urlencoded({ extended: false }))

    app.get("/", (req, res) => {
    res.status(200).send(`<h2>Guessing game</h2>
    Click here to get <a href="/addition">started</a>
    `)
    })

    app.get("/addition", (req, res) => {
    res.status(200).send(`<form method="POST" action="/result">
    <div>
    <label for="number1">Enter number 1</label>
    <input type="text" name="number1" />
    </div>
    <div>
    <label for="number2">Enter number 2</label>
    <input type="text" name="number2" />
    </div>
    <div>
    <input type="submit" value="Add" />
    </div>
    </form>`)
    })

    app.post("/result", (req, res) => {
    const value = +(req.body.number1) + (+(req.body.number2));
    res.status(200).send(`The result is ${value}. Click here to add some <a href="/addition">more</a>`)
    })
    app.listen(4000, () => {
    console.log(`Server started on port 4000`);
    })