Skip to content

Instantly share code, notes, and snippets.

@rajivnarayana
Created July 1, 2021 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajivnarayana/05b00f3dd836aa4da20b8ca6240a09ae to your computer and use it in GitHub Desktop.
Save rajivnarayana/05b00f3dd836aa4da20b8ca6240a09ae to your computer and use it in GitHub Desktop.
Simple express server to add 2 numbers
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`);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment