Skip to content

Instantly share code, notes, and snippets.

Avatar

hdd hdd42

  • United States
View GitHub Profile
@hdd42
hdd42 / app.js
Last active May 2, 2017
Promise+async/await
View app.js
async function getProducts(categoryId) {
let results = [];
let products = await ProductQuery(categoryId);
for (let i = 0; i < products.length; i++) {
let product = products[i]
let { images, fields } = await ImageQuery(product.productID)
let { options } = await OptionsOfProduct(product.productID)
View query_server.js
const app = require('http').createServer(handler)//hem socket hem web sunucu olarak calistirmak istedik
var io = require('socket.io')(app); // socket sunucusu
var mysql = require('mysql')//mysql;
const moment = require("moment") //tarih islemleri icin yardimci
var fs = require('fs');//html dosyamizi sunmak icin
app.listen(3001);// web ve socket sunucusunu istedigimiz port da baslatiyoruz(3001 gibi)
function handler (req, res) {
fs.readFile(__dirname + '/query.html',
View query.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
View model.js
/*
ilgili soruyu like yada unlike seklinde oylamak icin rota
router.route("/:id/vote")
.put(ctrl.errorHandler(ctrl.vote))
*/
// soru icin up yada down vote guncelleyen metod
async vote(req, res, next) {
let id = req.params.id;
let upOrDown = req.body.vote;
let userId= req.user._id
View gist:7644e5cd61af98ad32fbe6f8853eb4ce
// async / await version
exports.executeSQL = async (sql, parameters) => {
return new Promise((resolve, reject) => {
try {
let conn = new dbSQL.Connection(settings.dbConfig)
await conn.connect();
let req = new dbSQL.Request(conn);
View model.js
const mongoose = require('mongoose')
const ObjectId = mongoose.Schema.Types.ObjectId
//vrtual schema, post schema icinde sub field olarak kullanabilirsin.
const CommentSchema = new mongoose.Schema({
comment: {
type: String,
required: [true, "Bu alan Zorunludur!"]
},
user: { type: ObjectId, ref: 'User' },
View QuestionController.js
/**
* QuestionController
*
* @description :: Server-side logic for managing questions
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/
module.exports = {
current: function (req, res) {
var qId = req.body.id;
View app.js
//assets\js\app.js
//javascript i ayri bir dosyaya yazmis olduk
//daha rahat calismak icin bootstrap ve jquery ekledim
getQuestions();
var questions = []
var errorMessage = ""
var questionList = $('#questions')
var loading = $('#loading')
View homepage.ejs
<!-- burda ilk yukleme icin ejs ile sunu rendering de yapabilirsin,
sonrasinda kontrolu Javascript/Jquer veya angular vs ye birakabilirsin.
ama transfer edilecek veriler cok buyuk olmadigi icin biz
herseyi web socket uzerinden alip html i istemcide olusturuyoruz. -->
<div id="app" class>
<div class="row mb-2">
<div class="col-md-4">
View layout.ejs
<!DOCTYPE html>
<html>
<head>
<title><%=typeof title == 'undefined' ? 'New Sails App' : title%></title>
<!-- Viewport mobile tag for sensible mobile support -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<!--