Guest User

Untitled

a guest
Mar 7th, 2018
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 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:'bharathsanthakumar99',
  8. database:'bharathsanthakumar99',
  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. 'about':{
  19. title:'Bharath | about',
  20. heading:'Bharath',
  21. content:
  22. `<div class="container">
  23. <p> I am Bharath Santhakumar,</p>
  24. <p>S/o Santhakumar</p>
  25. <p>I am currently learning the basics of internet application and javascript</p>
  26. <p>This is all about me :-/</p>
  27. </div>`
  28.  
  29. },
  30.  
  31. 'education':{
  32. title:'Bharath | education',
  33. heading:'Bharath Santhakumar',
  34. content:
  35. `<div class="container">
  36. <table>
  37. <tbody>
  38. <tr>
  39. <th>SI.no</th>
  40. <th>Institution</th>
  41. </tr>
  42. <tr>
  43. <td>1</td>
  44. <td>Ayira vaisya Secondary school</td>
  45. </tr>
  46. <tr>
  47. <td>2</td>
  48. <td>Don Bosco Matriculation School</td>
  49. </tr>
  50. <tr>
  51. <td>3</td>
  52. <td>Srv matric higher secondary school</td>
  53. </tr>
  54. <tr>
  55. <td>4</td>
  56. <td>Sri manaukula vinayagar enginnering college</td>
  57. </tr>
  58. </tbody>
  59. </table>
  60. </div>`
  61. }
  62. };
  63.  
  64. function templates(data)
  65. {
  66. var title=data.title;
  67. var headings=data.heading;
  68. var content=data.content;
  69. var htmltemplate=`<!DOCTYPE html>
  70. <html>
  71. <head>
  72. <title>${title}</title>
  73. <link href="/ui/style.css" rel="stylesheet" />
  74. <meta name="viewport" content="width=device-width,initial-scale=1" />
  75. <link href="/ui/madi.png" rel="shortcut icon" />
  76.  
  77. </head>
  78. <body>
  79. ${headings}
  80. ${content}
  81. </body>
  82. </html>`;
  83. return (htmltemplate);
  84. }
  85.  
  86.  
  87. app.get('/', function (req, res) {
  88. res.sendFile(path.join(__dirname, 'ui', 'index.html'));
  89. });
  90.  
  91. var counter = 0;
  92. app.get('/counter',function (req,res){
  93. counter = counter + 1;
  94. res.send(counter.toString());
  95. });
  96.  
  97. var names =[];
  98.  
  99. app.get('/naming' , function(req,res) {
  100. var named=req.query.names;
  101. names.push(named);
  102. res.send(JSON.stringify(names));
  103.  
  104. });
  105.  
  106. app.get('/pic', function (req, res) {
  107. res.sendFile(path.join(__dirname, 'ui', 'pic.jpg'));
  108. });
  109.  
  110. var pool = Pool(config);
  111.  
  112. app.get('/articles/:articlename', function (req, res) {
  113.  
  114. pool.query("SELECT * FROM article WHERE title = $(1)"+[req.params.articlename],function(err,result) {
  115. if(err)
  116. {
  117. res.status(500).send(err.toString());
  118. }
  119. else
  120. {
  121. if(res.rows.length===0)
  122. {
  123. res.status(404).send("article is not there");
  124. }
  125. else
  126. {
  127. var articledata=result.rows[0];
  128. res.send(templates(articledata));
  129.  
  130. }
  131. }
  132. });
  133.  
  134.  
  135.  
  136. });
  137.  
  138. app.get('/ui/main.js', function (req, res) {
  139. res.sendFile(path.join(__dirname, 'ui', 'main.js'));
  140. });
  141.  
  142. app.get('/ui/style.css', function (req, res) {
  143. res.sendFile(path.join(__dirname, 'ui', 'style.css'));
  144. });
  145.  
  146. app.get('/ui/madi.png', function (req, res) {
  147. res.sendFile(path.join(__dirname, 'ui', 'madi.png'));
  148. });
  149.  
  150.  
  151.  
  152. // Do not change port, otherwise your app won't run on IMAD servers
  153. // Use 8080 only for local development if you already have apache running on 80
  154.  
  155. var port = 80;
  156. app.listen(port, function () {
  157. console.log(`IMAD course app listening on port ${port}!`);
  158. });
Add Comment
Please, Sign In to add comment