Last active
January 14, 2018 04:17
-
-
Save incorelabs/638be1473e2457e330d7efbc0e6d97ad to your computer and use it in GitHub Desktop.
FDWorkshop - Heroku API Server JS file
This file contains 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
const express = require("express"), | |
cors = require("cors"), | |
port = process.env.port || 3000, | |
app = express(); | |
app.use(cors()); | |
// assuming POST: name=foo&color=red <-- URL encoding | |
// | |
// or POST: {"name":"foo","color":"red"} <-- JSON encoding | |
app.use(express.json()); | |
app.use(express.urlencoded({ | |
extended: true | |
})); | |
app.get("/api-endpoint", (req, res) => { | |
console.log(req.query); | |
res.json({ | |
greeting: "Thank-you for registering using GET Request", | |
requestData: req.query | |
}); | |
}); | |
app.post("/api-endpoint", (req, res) => { | |
console.log(req.body); | |
res.json({ | |
greeting: "Thank-you for registering using POST Request", | |
requestData: req.body | |
}); | |
}); | |
if(!module.parent) { | |
app.listen(port, function() { | |
console.log('Express server listening on port ' + port + '.'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment