SHARE
TWEET

Untitled

a guest Jul 23rd, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require("express");
  2. const mongoose = require('mongoose');
  3.  
  4. const app= express()
  5.  
  6.  
  7.  
  8. app.use(express.json());
  9.  
  10.  
  11. let vcap_services = JSON.parse(process.env.VCAP_SERVICES);
  12. let replicaSetName = vcap_services['mongodb-innoworks'][0].credentials.replicaSetName;
  13. let db = vcap_services['mongodb-innoworks'][0].credentials.uri + '?replicaSet=' + replicaSetName;
  14.  
  15.  
  16. mongoose.connect(db)
  17.   .then(() => console.log('Connected to the MongoDB...'))
  18.   .catch(err => console.log('Could not connect to MongoDB...', err));
  19.  
  20.  
  21. const bookSchema = new mongoose.Schema({
  22.     date: Date,
  23.     topic:String,
  24.     data: Number,
  25.    
  26. },{versionKey:false});
  27.  
  28. const Bookmq = mongoose.model('Bookmq', bookSchema);
  29.  
  30.  
  31. app.get('/',(req,res)=>{
  32.     res.send('hello world');
  33. });
  34. app.get('/book',(req,res)=>{
  35.     Bookmq.find({}).then((err,books)=>{
  36.       if(err)
  37.         res.send(err);
  38.       else{
  39.         res.json(books);
  40.       }
  41.     });
  42. });
  43.  
  44. app.post('/api/book', (req, res) => {
  45.     const bookmq = new Bookmq({
  46.       date: req.body.date,
  47.       topic: req.body.topic,
  48.       data: req.body.data
  49.     });
  50.  
  51.     bookmq.save();
  52.     res.send(bookmq);
  53.   });
  54.  
  55.  
  56.  
  57. const port = process.env.port || 3000
  58. app.listen(port,(req,res)=>{
  59.     console.log(`listen port on ${port}`);
  60. });
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top