Guest User

Untitled

a guest
Mar 9th, 2018
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express');
  2. var morgan = require('morgan');
  3. var path = require('path');
  4.  
  5. var app = express();
  6. app.use(morgan('combined'));
  7.  
  8. var articles = {
  9.         'article-one': {
  10.             title: 'Article One | Abhishek Kansal',
  11.             heading: 'Article One',
  12.             date: 'March 10, 2018',
  13.             content: `
  14.             <p>
  15.                 This is the content for my first article.
  16.             </p>
  17.              <p>
  18.                 This is the content for my first article.
  19.             </p>
  20.              <p>
  21.                 This is the content for my first article.
  22.             </p>`
  23.         },
  24.         'article-two': {
  25.             title: 'Article Two | Abhishek Kansal',
  26.             heading: 'Article Two',
  27.             date: 'March 15, 2018',
  28.             content: `
  29.             <p>
  30.                 This is the content for my second article.
  31.             </p>
  32.              <p>
  33.                 This is the content for my second article.
  34.             </p>
  35.              <p>
  36.                 This is the content for my second article.
  37.             </p>`
  38.         },
  39.         'article-three': {
  40.             title: 'Article Three | Abhishek Kansal',
  41.             heading: 'Article Three',
  42.             date: 'March 20, 2018',
  43.             content: `
  44.             <p>
  45.                 This is the content for my third article.
  46.             </p>
  47.              <p>
  48.                 This is the content for my third article.
  49.             </p>
  50.              <p>
  51.                 This is the content for my third article.
  52.             </p>`
  53.         }
  54. };
  55.  
  56. function createTemplate(data){
  57.     var title = data.title;
  58.     var heading = data.heading;
  59.     var date = data.date;
  60.     var content = data.content;
  61.    
  62.     var htmlTemplate = `
  63.         <html>
  64.         <head>
  65.             <title>
  66.                 ${title}
  67.             </title>
  68.             <meta name="viewport" content="width=device-width, initial-scale=1" />
  69.              <link href="/ui/style.css" rel="stylesheet" />
  70.         </head>
  71.         <body>
  72.             <div class="container">
  73.                 <div>
  74.                     <a href="/">Home</a>
  75.                 </div>
  76.                 <hr/>
  77.                 <h3>
  78.                     ${heading}
  79.                 </h3>
  80.                 <div>
  81.                     ${date}
  82.                 </div>
  83.                 <div>
  84.                     ${content}
  85.                 </div>
  86.             </div>
  87.         </body>
  88.     </html>
  89.    
  90.     `;
  91.     return htmlTemplate;
  92. }
  93. app.get('/', function (req, res) {
  94.   res.sendFile(path.join(__dirname, 'ui', 'index.html'));
  95. });
  96.  
  97. app.get('/:articleName', function (req, res) {
  98.     var articleName = req.params.articleName;
  99.     res.send(createTemplate(articles[articleName]));
  100. });
  101.  
  102.  
  103. app.get('/ui/style.css', function (req, res) {
  104.   res.sendFile(path.join(__dirname, 'ui', 'style.css'));
  105. });
  106.  
  107. app.get('/ui/main.js', function (req, res) {
  108.   res.sendFile(path.join(__dirname, 'ui', 'main.js'));
  109. });
  110.  
  111. app.get('/ui/madi.png', function (req, res) {
  112.   res.sendFile(path.join(__dirname, 'ui', 'madi.png'));
  113. });
  114.  
  115.  
  116. // Do not change port, otherwise your app won't run on IMAD servers
  117. // Use 8080 only for local development if you already have apache running on 80
  118.  
  119. var port = 80;
  120. app.listen(port, function () {
  121.   console.log(`IMAD course app listening on port ${port}!`);
  122. });
Add Comment
Please, Sign In to add comment