Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express');
  2. var app = express();
  3. var bodyParser = require('body-parser');
  4. var mongoose = require('mongoose');
  5. var Name = require('./Name.model');
  6. var bootstrap = `<head>
  7.                     <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  8.                 </head>`;
  9.  
  10. // parse application/x-www-form-urlencoded
  11. app.use(bodyParser.urlencoded({ extended: true }))
  12.  
  13. // parse application/json
  14. app.use(bodyParser.json())
  15.  
  16. var db = 'mongodb://test:test@ds135577.mlab.com:35577/nodepractice';
  17.  
  18. mongoose.connect(db);
  19.  
  20. var port = 8080;
  21.  
  22. app.get('/', function(req,res) {
  23.     res.send(`
  24.  
  25.         ${bootstrap}
  26.         <style>
  27.             #form {
  28.                 text-align: center
  29.             }
  30.         </style>
  31.         <body>
  32.         <div id="form">
  33.             <form action='/name' method='post'>
  34.                 <h2>name:</h2>
  35.                 <input type="text" name="name">
  36.                 <br/>
  37.                 <h2>age:</h2>
  38.                 <input type="age" name="age">
  39.                 <br/><br/>
  40.                 <input type="submit" value="submit">
  41.             </form>
  42.         </div>
  43.  
  44.             <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  45.         </body>
  46.  
  47.         `);
  48. });
  49.  
  50.  
  51. app.get('/names', function(req,res) {
  52.     Name.find({})
  53.         .exec(function(err,names) {
  54.             if (err) {
  55.                 res.send('An error has occured');
  56.             } else {
  57.                 res.writeHead(200, {'Content-type': 'text/html'});
  58.                 res.write(` ${bootstrap}
  59.                             <style>
  60.                                 table {
  61.                                     margin: 1em auto;
  62.                                     width: 25%;
  63.                                 }
  64.  
  65.                                 #table {
  66.                                     text-align: center;
  67.                                 }
  68.  
  69.                                 td, th {
  70.                                 border: 1px solid #dddddd;
  71.                                 text-align: left;
  72.                                 padding: 8px;
  73.                             }
  74.  
  75.                                 tr:nth-child(even) {
  76.                                 background-color: #dddddd;
  77.                             }
  78.  
  79.                             </style>
  80.                             <div id='table'>
  81.                             <table>
  82.                             <tr>
  83.                                 <th>name</th>
  84.                                 <th>age</th>
  85.                             </tr>
  86.                         `)
  87.  
  88.                 names.forEach(function(n) {
  89.                     if(n.name){
  90.                         res.write(`     <tr>
  91.                                             <td>${n.name}</td>
  92.                                             <td>${n.age}</td>
  93.                                         </tr>`);
  94.                     }
  95.                 });
  96.                 res.write(`</table><br />
  97.                             <a href='/'>Home</a></div>`)
  98.                 res.end();
  99.             }
  100.         });
  101. });
  102.  
  103. app.post('/name', function(req,res) {
  104.     var newName = new Name();
  105.  
  106.     newName.name = req.body.name;
  107.     newName.age = req.body.age;
  108.  
  109.     newName.save(function(err, name) {
  110.         if (err) {
  111.             res.send('error: ${err}');
  112.         } else {
  113.             console.log(name);
  114.             res.redirect('/names');
  115.         }
  116.     });
  117. });
  118.  
  119. app.listen(port, function() {
  120.     console.log('Listening on port 8080'); 
  121. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement