jazst21
2/8/2019 - 6:41 AM

sample mongoose express

const express = require('express')
const app = express()
const port = 3000
const faker = require('faker');
const Sequelize = require('sequelize');

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test', {
  useNewUrlParser: true
});

const Cat = mongoose.model('Cat', {
  name: String
});
const messages = mongoose.model(
  'message', {
    user: String,
    time: Date,
    mesej: String,
    star: false
  }
)
const kitty = new Cat({
  name: 'Zildjian'
});
const User1Mesej = new messages({
  user: 'pugar',
  mesej: faker.lorem.lines(),
  time: faker.date.recent()
})

var namaObj = {
  "obj1": "hehe",
  "obj2": 1,
  angka: 100,
  "job": faker.name.jobTitle()
};

app.get('/', (req, res) => res.send('Hello World!'))
app.get('/nama', (req, res) => {
  console.log(JSON.stringify(namaObj));
  console.log("hehehe");
  kitty.save().then(() => console.log('meow'));
  User1Mesej.save()
    .then(
      () => {
        console.log('comment:' + User1Mesej.mesej + ' sent by ' + User1Mesej.user + ' at ' + User1Mesej.time);
      }
    )
    .catch(
      (error) => {
        console.log(error)
      }
    );
  res.send(
    namaObj
  )
})

app.get('/auto', (req,res)=>{
   var i;
   for (i = 0; i < 3; i++) {
    const UserXmesej = new messages({
      user: faker.name.firstName(),
      mesej: faker.lorem.lines(),
      time: faker.date.recent()
    });
    UserXmesej.save().then((i)=>console.log('input number -'+i)).catch((error)=>console.log(error))
   };
   console.log('sent');
   res.send('posted');
})

app.get('/list',(req,res)=>{
  var listall = messages.find().exec();
  res.send("listall");
  console.log(listall);
  messages.find().exec()
  .then((res,error)=>{
    console.log(res)
  })
})

app.listen(port, () => console.log(`Example app listening on port ${port}!`))