Commit 8cf231dd authored by GanJc's avatar GanJc

user api

parent 5fa006c7
...@@ -18,7 +18,7 @@ app.set('view engine', 'jade'); ...@@ -18,7 +18,7 @@ app.set('view engine', 'jade');
//allow custom header and CORS //allow custom header and CORS
app.all('*',function (req, res, next) { app.all('*',function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild'); // res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS'); res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
if (req.method == 'OPTIONS') { if (req.method == 'OPTIONS') {
res.send(200); res.send(200);
......
This diff is collapsed.
module.exports = require('./lib/eventproxy');
This diff is collapsed.
{
"_args": [
[
"eventproxy",
"/Users/linus/learn/ygo/ygopro-arena-api"
]
],
"_cnpm_publish_time": 1468983521805,
"_from": "eventproxy@latest",
"_id": "eventproxy@0.3.5",
"_inCache": true,
"_installable": true,
"_location": "/eventproxy",
"_nodeVersion": "4.4.4",
"_npmOperationalInternal": {
"host": "packages-12-west.internal.npmjs.com",
"tmp": "tmp/eventproxy-0.3.5.tgz_1468983520153_0.16356190340593457"
},
"_npmUser": {
"email": "shyvo1987@gmail.com",
"name": "jacksontian"
},
"_npmVersion": "2.15.1",
"_phantomChildren": {},
"_requested": {
"name": "eventproxy",
"raw": "eventproxy",
"rawSpec": "",
"scope": null,
"spec": "latest",
"type": "tag"
},
"_requiredBy": [
"/"
],
"_resolved": "https://registry.npm.taobao.org/eventproxy/download/eventproxy-0.3.5.tgz",
"_shasum": "4db3290dcbc51cf067cbb6752e3c40b5d917212f",
"_shrinkwrap": null,
"_spec": "eventproxy",
"_where": "/Users/linus/learn/ygo/ygopro-arena-api",
"author": {
"email": "shyvo1987@gmail.com",
"name": "Jackson Tian"
},
"bugs": {
"url": "https://github.com/JacksonTian/eventproxy/issues"
},
"contributors": [
{
"email": "shyvo1987@gmail.com",
"name": "Jackson Tian"
},
{
"email": "fengmk2@gmail.com",
"name": "fengmk2"
}
],
"dependencies": {
"debug": "2.2.0"
},
"description": "An implementation of task/event based asynchronous pattern.",
"devDependencies": {
"browser": "0.2.6",
"contributors": "*",
"growl": "1.9.2",
"istanbul": "*",
"mocha": "2.5.3",
"pedding": "1.0.0"
},
"directories": {
"test": "test"
},
"dist": {
"noattachment": false,
"shasum": "4db3290dcbc51cf067cbb6752e3c40b5d917212f",
"size": 10722,
"tarball": "http://registry.npm.taobao.org/eventproxy/download/eventproxy-0.3.5.tgz"
},
"files": [
"lib",
"index.js"
],
"gitHead": "410b8a2021be9e1ed6ae396483bfb2ebe34f223e",
"homepage": "https://github.com/JacksonTian/eventproxy",
"keywords": [
"event",
"task-base",
"event machine",
"nested callback terminator"
],
"license": "MIT",
"main": "index.js",
"maintainers": [
{
"email": "shyvo1987@gmail.com",
"name": "jacksontian"
}
],
"name": "eventproxy",
"optionalDependencies": {},
"publish_time": 1468983521805,
"readme": "ERROR: No README data found!",
"repository": {
"type": "git",
"url": "git://github.com/JacksonTian/eventproxy.git"
},
"scripts": {
"test": "make test-all"
},
"spm": {
"ignore": [
"test"
],
"main": "index.js"
},
"version": "0.3.5"
}
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
"body-parser": "~1.15.2", "body-parser": "~1.15.2",
"cookie-parser": "~1.4.3", "cookie-parser": "~1.4.3",
"debug": "~2.2.0", "debug": "~2.2.0",
"eventproxy": "^0.3.5",
"express": "~4.14.0", "express": "~4.14.0",
"jade": "~1.11.0", "jade": "~1.11.0",
"morgan": "~1.7.0", "morgan": "~1.7.0",
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
var pg = require('pg'); var pg = require('pg');
var eventproxy = require('eventproxy');
// create a config to configure both pooling behavior // create a config to configure both pooling behavior
// and client options // and client options
...@@ -66,4 +67,97 @@ router.get('/users', function (req, res) { ...@@ -66,4 +67,97 @@ router.get('/users', function (req, res) {
}) })
}); });
router.get('/user', function (req, res) {
// to run a query we can acquire a client from the pool,
// run a query on the client, and then return the client to the pool
pool.connect(function (err, client, done) {
if (err) {
return console.error('error fetching client from pool', err);
}
var username = req.query.username ;
if(!username){
res.json({message: "error, username require"});
return
}
var resultData = {
'exp': '800',
'exp_rank': '0',
'pt': '800',
'arena_rank': '0',
'win': '0',
'lose': '0',
'draw': '0',
'all': '0',
'ratio': '0'
}
var sql = `SELECT * from rating_index where username = '${username}'`
console.log(sql);
client.query(sql, function (err, result) {
//call `done()` to release the client back to the pool
done();
if (err) {
return console.error('error running query', err);
}
if(result.rows.length === 0){
res.json({message: "error, user not found"});
}else {
var user = result.rows[0]
resultData['exp'] = user['exp']
resultData['pt'] = user['pt']
resultData['win'] = user['win']
resultData['lose'] = user['lose']
resultData['all'] = user['game']
resultData['draw'] = user['game'] - user['lose'] - user['win']
resultData['ratio'] = (user['win'] / user['game'] * 100 ).toFixed(2)
var ep = new eventproxy();
ep.after('delay', 2, function (row) {
console.log('final:');
console.log(resultData);
res.json(resultData);
});
client.query(`SELECT count(*) from rating_index where pt >= ${resultData['pt']}`, function (err, result) {
done();
if (err) {
return console.error('error running query', err);
}
resultData['arena_rank'] = result.rows[0]['count']
ep.emit('delay', '');
});
client.query(`SELECT count(*) from rating_index where exp >= ${resultData['exp']}`, function (err, result) {
done();
if (err) {
return console.error('error running query', err);
}
resultData['exp_rank'] = result.rows[0]['count']
ep.emit('delay', '');
});
}
});
});
pool.on('error', function (err, client) {
// if an error is encountered by a client while it sits idle in the pool
// the pool itself will emit an error event with both the error and
// the client which emitted the original error
// this is a rare occurrence but can happen if there is a network partition
// between your application and the database, the database restarts, etc.
// and so you might want to handle it and at least log it out
console.error('idle client error', err.message, err.stack)
res.json({message: "error"});
})
});
module.exports = router; module.exports = router;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment