Skip to content

Instantly share code, notes, and snippets.

@malikkurosaki
Created June 23, 2019 10:50
Show Gist options
  • Save malikkurosaki/ef55fa82fdb74d3c6f2316ebed80d77e to your computer and use it in GitHub Desktop.
Save malikkurosaki/ef55fa82fdb74d3c6f2316ebed80d77e to your computer and use it in GitHub Desktop.
node js kita sehat

node js kita sehat

const express = require('express')
const path = require('path')
const PORT = process.env.PORT || 5000
const mysql = require('mysql')
const parser = require('body-parser')
const admin = require('firebase-admin')
const https = require('https')

const serviceAccount = require("./firebase_key.json");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://kitasehat-3f899.firebaseio.com"
});

const db = mysql.createConnection({
  host:"bestmedialearning.com",
  user:"u6045499_apibest",
  password:"makuro123",
  database:"u6045499_kitasehat"
})
db.connect((err)=>{
  if(err) throw err
})
process.on('uncaughtException',(err)=>{
  if(err){
    console.log(err)
  }
})

const app = express();
app.use(parser.json())
app.use(parser.urlencoded({extended:true}))

app.set('view engine','pug')
app.set('views','./views')

app.get('/',(req,res)=>{
  res.render('index',{
    title:"kita sehat"
  })
})

app.post('/api/newpost',(req,res)=>{
  var terima = req.body
    let sql = `insert into content_sehat(id,judul,isi) values(null,"${terima.title}","${terima.body}")`
    let query = db.query(sql,(c,d)=>{
      if(c)throw c

      //payload
      var payload = {
        notification: {
            title:terima.title,
            body:terima.body,
            sound: "default"
        },
        data:{
            title:terima.title,
            body:terima.body
        }
    }

    admin.messaging().sendToTopic("berita",payload).then((response) => {
    // Response is a message ID string.
          res.send(response)
      }).catch((error) => {
          res.send(error)
      });
    })  
})

app.get('/api/content',(a,b)=>{
  let terima = a.body
  let sql = 'select * from content_sehat'
  let query = db.query(sql,(c,d)=>{
    if(c)throw c
    b.send(d)
  })
})



setInterval(()=>{
  db.query('select 1')
  https.get('https://kitasehat.herokuapp.com/')
},1000)
app.listen(PORT,()=>{
  console.log(`app run on port : ${PORT}`)
})

pug lauout

doctype html
html
    title=title
    link(rel="stylesheet", href="https://www.w3schools.com/w3css/4/w3.css")
    script(src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js")
    style.
        .w3-container{
            padding:0px;
        }

    body.w3-container.w3-content
        h1 #{title}
        .w3-input.w3-blue-grey.w3-xlarge new post
        input#judul(type="text" placeholder="input title").w3-input.w3-light-grey
        textarea#isi(name="", cols="30", rows="10" placeholder="input your content here").w3-input.w3-light-grey
        #save.w3-button.w3-blue-grey.w3-right( onclick="kirim()") save
        #hasil.w3-container.w3-padding
        
        script.
            function kirim(){
                var jd = $("#judul").val()
                var bd =  $("#isi").val()

                if (jd == "" || bd == ""){
                    alert("field not be empty ok !")
                    return;
                }

                var paketan = {
                    title:jd,
                    body:bd
                }

                $.post('/api/newpost',paketan,(data,status)=>{
                    alert(status)
                    location.reload()
                })
            }
            $.get('/api/content',(a,b)=>{
                var terima = "";
                for (var i = 0;i< a.length;i++){
                    terima += `${i+1} <a href="#" class=''>${a[i].judul}</a><br>`
                }
                $("#hasil").append(terima)
            })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment