Guest User

Untitled

a guest
Oct 9th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. "use strict";
  2. /* jshint node:true */
  3.  
  4. // Add the express web framework
  5. const express = require("express");
  6. const app = express();
  7. const { URL } = require("url");
  8.  
  9. // Use body-parser to handle the PUT data
  10. const bodyParser = require("body-parser");
  11. app.use(
  12. bodyParser.urlencoded({
  13. extended: false
  14. })
  15. );
  16.  
  17. // Then we'll pull in the database client library
  18. const { Etcd3 } = require("etcd3");
  19.  
  20. let endpoints = process.env.COMPOSE_ETCD_ENDPOINTS;
  21.  
  22. if (endpoints === undefined) {
  23. console.error("Please set the COMPOSE_ETCD_ENDPOINTS environment variable");
  24. process.exit(1);
  25. }
  26.  
  27. let envuser = process.env.COMPOSE_ETCD_USER;
  28. let envpass = process.env.COMPOSE_ETCD_PASS;
  29.  
  30. // Create auth credentials
  31. let opts = {
  32. hosts: endpoints.split(","),
  33. auth: {
  34. username: envuser,
  35. password: envpass
  36. }
  37. };
  38.  
  39. var etcd = new Etcd3(opts).namespace("/grand_tour/words/");
  40.  
  41. // We want to extract the port to publish our app on
  42. let port = process.env.PORT || 8080;
  43.  
  44. // We can now set up our web server. First up we set it to serve static pages
  45. app.use(express.static(__dirname + "/public"));
  46.  
  47. // Add a word to the database
  48. function addWord(word, definition) {
  49. return etcd.put(word).value(definition);
  50. }
  51.  
  52. // Get words from the database
  53. function getWords() {
  54. return etcd
  55. .getAll()
  56. .strings()
  57. .then(values => {
  58. return new Promise((resolve, reject) => {
  59. let words = [];
  60. for (const key in values) {
  61. words.push({ word: key, definition: values[key] });
  62. }
  63. resolve(words);
  64. });
  65. });
  66. }
  67.  
  68. // The user has clicked submit to add a word and definition to the database
  69. // Send the data to the addWord function and send a response if successful
  70. app.put("/words", function(request, response) {
  71. addWord(request.body.word, request.body.definition)
  72. .then(function(resp) {
  73. response.send(resp);
  74. })
  75. .catch(function(err) {
  76. console.log(err);
  77. response.status(500).send(err);
  78. });
  79. });
  80.  
  81. // Read from the database when the page is loaded or after a word is successfully added
  82. // Use the getWords function to get a list of words and definitions from the database
  83. app.get("/words", function(request, response) {
  84. getWords()
  85. .then(function(words) {
  86. response.send(words);
  87. })
  88. .catch(function(err) {
  89. console.log(err);
  90. response.status(500).send(err);
  91. });
  92. });
  93.  
  94. // Listen for a connection.
  95. app.listen(port, function() {
  96. console.log("Server is listening on port " + port);
  97. });
Add Comment
Please, Sign In to add comment