Guest User

Untitled

a guest
Jan 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. var bodyParser = require('body-parser');
  2. var express = require('express');
  3. var app = express();
  4.  
  5. // --> 7) Mount the Logger middleware here
  6. app.use(function(req, res, next){
  7. console.log(req.method + " " + req.path + " - " + req.ip);
  8. next();
  9. })
  10.  
  11. // --> 11) Mount the body-parser middleware here
  12. app.use(function(req, res, next){
  13. bodyParser.urlencoded({extended: false})
  14. next()
  15. })
  16.  
  17. /** 1) Meet the node console. */
  18. // console.log("Hello World")
  19.  
  20.  
  21. /** 2) A first working Express Server */
  22. // app.get('/', function(req, res){
  23. // res.send("Hello Express")
  24. // })
  25.  
  26. /** 3) Serve an HTML file */
  27. app.get('/', function(req, res){
  28. res.sendFile(__dirname + '/views/index.html')
  29. })
  30.  
  31. /** 4) Serve static assets */
  32. app.use(express.static(__dirname + '/public'))
  33.  
  34. /** 5) serve JSON on a specific route */
  35.  
  36. // app.get('/json', (req, res) => {
  37. // res.json({"message": "Hello json"})
  38. // })
  39.  
  40. /** 6) Use the .env file to configure the app */
  41. app.get('/json', (req, res) => {
  42. let message = "Hello json"
  43. if(process.env.MESSAGE_STYLE=='uppercase'){
  44. message = message.toUpperCase()
  45. }
  46. res.json({"message": message})
  47. })
  48.  
  49. /** 7) Root-level Middleware - A logger */
  50. // place it before all the routes !
  51. // app.use(function(req, res, next){
  52. // console.log(req.method + " " + req.path + " " + req.ip)
  53. // })
  54.  
  55. /** 8) Chaining middleware. A Time server */
  56. app.get('/now', (req, res, next) => {
  57. req.time = new Date().toString()
  58. next()
  59. }, (req, res) => {
  60. res.json({"time": req.time})
  61. })
  62.  
  63. /** 9) Get input from client - Route parameters */
  64. app.get('/:word/echo', (req, res)=> {
  65. res.json({"echo": req.params.word})
  66. })
  67.  
  68. /** 10) Get input from client - Query parameters */
  69. // /name?first=<firstname>&last=<lastname>
  70. app.route('/name')
  71. .get((req, res)=> {
  72. res.json({"name": req.query.first + " " + req.query.last})
  73. })
  74. .post((req, res)=> {
  75. res.json({"name": req.query.first + " " + req.query.last})
  76. })
  77.  
  78. /** 11) Get ready for POST Requests - the `body-parser` */
  79. // place it before all the routes !
  80.  
  81.  
  82. /** 12) Get data form POST */
  83.  
  84.  
  85.  
  86. // This would be part of the basic setup of an Express app
  87. // but to allow FCC to run tests, the server is already active
  88. /** app.listen(process.env.PORT || 3000 ); */
  89.  
  90. //---------- DO NOT EDIT BELOW THIS LINE --------------------
  91.  
  92. module.exports = app;
Add Comment
Please, Sign In to add comment