Guest User

Untitled

a guest
Mar 6th, 2018
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 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. username:'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.  
  111. var pool=new pool(config);
  112.  
  113. app.get('/articles/:articlename', function (req, res) {
  114.  
  115. pool.query("SELECT * FROM article WHERE title = $(1)"+[req.params.articlename],function(err,result) {
  116. if(err)
  117. {
  118. res.status(500).send(err.toString());
  119. }
  120. else
  121. {
  122. if(res.rows.length===0)
  123. {
  124. res.status(404).send("article is not there");
  125. }
  126. else
  127. {
  128. var articledata=result.rows[0];
  129. res.send(templates(articledata));
  130.  
  131. }
  132. }
  133. });
  134.  
  135.  
  136.  
  137. });
  138.  
  139. app.get('/ui/main.js', function (req, res) {
  140. res.sendFile(path.join(__dirname, 'ui', 'main.js'));
  141. });
  142.  
  143. app.get('/ui/style.css', function (req, res) {
  144. res.sendFile(path.join(__dirname, 'ui', 'style.css'));
  145. });
  146.  
  147. app.get('/ui/madi.png', function (req, res) {
  148. res.sendFile(path.join(__dirname, 'ui', 'madi.png'));
  149. });
  150.  
  151.  
  152.  
  153. // Do not change port, otherwise your app won't run on IMAD servers
  154. // Use 8080 only for local development if you already have apache running on 80
  155.  
  156. var port = 80;
  157. app.listen(port, function () {
  158. console.log(`IMAD course app listening on port ${port}!`);
  159. });
Add Comment
Please, Sign In to add comment