Created
June 28, 2018 17:16
-
-
Save yukeehan/a3910956ea777ea7dee74af78f12fdeb to your computer and use it in GitHub Desktop.
Post Request Demo
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
var express = require("express"); | |
var app = express(); | |
var bodyParser = require("body-parser"); | |
app.set("view engine", "ejs"); | |
app.use(bodyParser.urlencoded({extend:true})); | |
var friends = ["Yukee", "Mountain", "Xiaobai", "Tony"]; | |
app.get("/",function(req, res){ | |
res.render("home"); | |
}); | |
app.post("/addfriend", function(req, res){ | |
var newFriend = req.body.name; | |
friends.push(newFriend); | |
res.redirect("/friends"); | |
}); | |
app.get("/friends", function(req, res){ | |
res.render("friends",{friends:friends}); | |
}); | |
app.listen(process.env.PORT, process.env.IP, function(){ | |
console.log("server started!"); | |
}); |
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
<h1>Friends List</h1> | |
<% friends.forEach(function(friend){ %> | |
<li><%= friend %></li> | |
<% }); %> | |
<form action="/addfriend" method="POST"> | |
<input type="text" name="name" placeholder="name"> | |
<button>I made a new friend!</button> | |
</form> |
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
<h1>This is the homepage</h1> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment