Guest User

Untitled

a guest
Mar 17th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. var express = require('express');
  2. var morgan = require('morgan');
  3. var path = require('path');
  4. var Pool = require('pg').Pool;
  5.  
  6. var config = {
  7. user: 'abhishekkansal97',
  8. database: 'abhishekkansal97',
  9. host: 'db.imad.hasura-app.io',
  10. port: '5432',
  11. password: process.env.DB_PASSWORD
  12. };
  13.  
  14. var app = express();
  15. app.use(morgan('combined'));
  16.  
  17. var articles = {
  18. 'article-one': {
  19. title: 'Article One | Abhishek Kansal',
  20. heading: 'Article One',
  21. date: 'March 10, 2018',
  22. content: `
  23. <p>
  24. This is the content for my first article.
  25. </p>
  26. <p>
  27. This is the content for my first article.
  28. </p>
  29. <p>
  30. This is the content for my first article.
  31. </p>`
  32. },
  33. 'article-two': {
  34. title: 'Article Two | Abhishek Kansal',
  35. heading: 'Article Two',
  36. date: 'March 15, 2018',
  37. content: `
  38. <p>
  39. This is the content for my second article.
  40. </p>
  41. <p>
  42. This is the content for my second article.
  43. </p>
  44. <p>
  45. This is the content for my second article.
  46. </p>`
  47. },
  48. 'article-three': {
  49. title: 'Article Three | Abhishek Kansal',
  50. heading: 'Article Three',
  51. date: 'March 20, 2018',
  52. content: `
  53. <p>
  54. This is the content for my third article.
  55. </p>
  56. <p>
  57. This is the content for my third article.
  58. </p>
  59. <p>
  60. This is the content for my third article.
  61. </p>`
  62. }
  63. };
  64.  
  65. function createTemplate(data){
  66. var title = data.title;
  67. var heading = data.heading;
  68. var date = data.date;
  69. var content = data.content;
  70.  
  71. var htmlTemplate = `
  72. <html>
  73. <head>
  74. <title>
  75. ${title}
  76. </title>
  77. <meta name="viewport" content="width=device-width, initial-scale=1" />
  78. <link href="/ui/style.css" rel="stylesheet" />
  79. </head>
  80. <body>
  81. <div class="container">
  82. <div>
  83. <a href="/">Home</a>
  84. </div>
  85. <hr/>
  86. <h3>
  87. ${heading}
  88. </h3>
  89. <div>
  90. ${date.toDateString()}
  91. </div>
  92. <div>
  93. ${content}
  94. </div>
  95. </div>
  96. </body>
  97. </html>
  98.  
  99. `;
  100. return htmlTemplate;
  101. }
  102. app.get('/', function (req, res) {
  103. res.sendFile(path.join(__dirname, 'ui', 'index.html'));
  104. });
  105.  
  106. var pool = new Pool(config);
  107. app.get('/test-db', function (req, res) {
  108. //make a select request
  109. //return a response
  110. pool.query('SELECT * FROM test', function(err, result){
  111. if (err) {
  112. res.status(500).send(err.toString);
  113. }
  114. else{
  115. res.send(JSON.stringify(result.rows));
  116. }
  117. });
  118. });
  119.  
  120. var counter = 0;
  121. app.get('/counter', function (req, res) {
  122. counter=counter + 1;
  123. res.send(counter.toString());
  124. });
  125.  
  126. var names = [];
  127. app.get('/submit-name', function(req, res) { //url: /submit-name?name=abhi
  128. //Get the name from the request
  129. var name = req.query.name;
  130. names.push(name);
  131. // JSON: Javascript Object Notation
  132. res.send(JSON.stringify(names));
  133. });
  134.  
  135.  
  136. app.get('/articles/:articleName', function (req, res) {
  137. // var articleName = req.params.articleName;
  138.  
  139. // SELECT * FROM article WHERE title = 'article-one'
  140. pool.query("SELECT * FROM article WHERE title = '"+req.params.articleName + "'", function (err, result) {
  141. if (err) {
  142. res.status(500).send(err.toString());
  143. }
  144. else{
  145. if(result.rows.length === 0){
  146. res.status(404).send('Article not found');
  147. }
  148. else {
  149. var articleData = result.rows[0];
  150. res.send(createTemplate(articleData));
  151. }
  152. }
  153.  
  154. });
  155.  
  156. // res.send(createTemplate(articleData));
  157. });
  158.  
  159.  
  160. app.get('/ui/style.css', function (req, res) {
  161. res.sendFile(path.join(__dirname, 'ui', 'style.css'));
  162. });
  163.  
  164. app.get('/ui/main.js', function (req, res) {
  165. res.sendFile(path.join(__dirname, 'ui', 'main.js'));
  166. });
  167.  
  168. app.get('/ui/madi.png', function (req, res) {
  169. res.sendFile(path.join(__dirname, 'ui', 'madi.png'));
  170. });
  171.  
  172.  
  173.  
  174. // Do not change port, otherwise your app won't run on IMAD servers
  175. // Use 8080 only for local development if you already have apache running on 80
  176.  
  177. var port = 80;
  178. app.listen(port, function () {
  179. console.log(`IMAD course app listening on port ${port}!`);
  180. });
Add Comment
Please, Sign In to add comment