Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Here is the code for the app
  2. //app.js
  3.  
  4. var express = require('express');
  5. var app = express();
  6. var bodyParser = require('body-parser');
  7. var mongoose = require('mongoose');
  8. var Name = require('./Name.model');
  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.         <head>
  26.             <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  27.         </head>
  28.         <style>
  29.             #form {
  30.                 text-align: center
  31.             }
  32.         </style>
  33.         <body>
  34.         <div id="form">
  35.             <form action='/name' method='post'>
  36.                 <h2>name:</h2>
  37.                 <input type="text" name="name">
  38.                 <br/>
  39.                 <h2>age:</h2>
  40.                 <input type="age" name="age">
  41.                 <br/><br/>
  42.                 <input type="submit" value="submit">
  43.             </form>
  44.         </div>
  45.  
  46.             <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  47.         </body>
  48.  
  49.         `);
  50. });
  51.  
  52.  
  53. app.get('/names', function(req,res) {
  54.     Name.find({})
  55.         .exec(function(err,names) {
  56.             if (err) {
  57.                 res.send('An error has occured');
  58.             } else {
  59.                 res.json(names);
  60.             }
  61.         });
  62. });
  63.  
  64. app.post('/name', function(req,res) {
  65.     var newName = new Name();
  66.  
  67.     newName.name = req.body.name;
  68.     newName.age = req.body.age;
  69.  
  70.     newName.save(function(err, name) {
  71.         if (err) {
  72.             res.send('error: ${err}');
  73.         } else {
  74.             console.log(name);
  75.             res.send(name);
  76.         }
  77.     });
  78. });
  79.  
  80. app.listen(port, function() {
  81.     console.log('Listening on port 8080'); 
  82. });
  83.  
  84.  
  85. //Here is the code for the db schema
  86. //Name.model.js
  87. var mongoose = require('mongoose');
  88. var Schema = mongoose.Schema;
  89.  
  90. var nameSchema = new Schema({
  91.     name : String,
  92.     age  : Number
  93. });
  94.  
  95. module.exports = mongoose.model('name', nameSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement