daily pastebin goal
4%
SHARE
TWEET

Untitled

a guest Aug 22nd, 2018 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.     "_id": {
  3.         "$oid": "5b7d0f70f097a71eccb9b360"
  4.     },
  5.     "__v": 0
  6. }
  7.    
  8. var mongoose = require('mongoose');
  9. var Schema = mongoose.Schema;
  10.  
  11. var UserSchema = new Schema({
  12.     name: String,
  13.     comment: String,
  14.     date: String
  15. });
  16.  
  17. module.exports = mongoose.model("User", UserSchema);
  18.    
  19. var express = require('express');
  20. var morgan = require('morgan');
  21. var bodyParser = require('body-parser');
  22. var config = require('./config');
  23. var mongoose = require('mongoose');
  24.  
  25. var app = express();
  26.  
  27. app.use(bodyParser.urlencoded({ extended: true }));
  28. app.use(bodyParser.json());
  29. app.use(morgan('dev'));
  30.  
  31. var api = require("./app/routes/api")(app, express);
  32.  
  33. app.use('/api', api);
  34.  
  35. app.get('*',(req, res)=>{
  36.     res.sendFile(__dirname + '/public/views/index.html');
  37. });
  38.  
  39. mongoose.connect(config.database, { useNewUrlParser: true },(err)=>{
  40.     if(err){
  41.         console.log(err);
  42.     }else{
  43.         console.log('Connected to DB');
  44.     }
  45. });
  46.  
  47.  
  48.  
  49. app.listen(config.port, (err) =>{
  50.     if(err){
  51.         console.log(err);
  52.     }else{
  53.         console.log('Listening on port:'+ config.port);
  54.     }
  55. });
  56.    
  57. var User = require('../models/user');
  58.  
  59.  
  60. module.exports = function(app,express){
  61.  
  62.     var api = express.Router();
  63.  
  64.     api.post('/comment', function(req,res){
  65.         var user = new User({
  66.             name: req.body.name,
  67.             comment: req.body.comment,
  68.             date: req.body.date
  69.         });
  70.  
  71.         user.save(function(err){
  72.             if(err){
  73.                 res.send(err);
  74.                 return;
  75.             }
  76.  
  77.             res.json({ message: "User has commented"});
  78.         });
  79.     });
  80.  
  81.     return api;
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top