Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
REPLで作ったハリボテAPIクライアント
'use strict';
var http = require('http');
module.exports = class ApiClient {
constructor(host, port) {
this.host = host;
this.port = port;
}
setHost(host) {
this.host = host;
return `host = [${this.host}]`;
}
setPort(port) {
this.port = port;
return `port = [${this.port}]`;
}
getHost() {
return `host = [${this.host}]`;
}
getPort() {
return `port = [${this.port}]`;
}
authenticate(identifier, password) {
var req = http.request({
hostname: this.host,
port: this.port,
path: '/api/authenticate/token',
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
}, res => {
res.on('data', chunk => {
var response = JSON.parse(chunk.toString());
this.JWT = response.JWT;
});
res.on('end', () => console.log(`get JWT ${this.JWT}`));
});
req.on('error', err => console.log(err));
req.write(JSON.stringify({ identifier, password }));
req.end();
}
getJWT() {
return `JWT = ${this.JWT}`;
}
}
#!/usr/bin/env node
var repl = require('repl');
var env = process.env.NODE_ENV || 'development';
var dbconf = require('../config/database')[env];
var Sequelize = require('sequelize');
var sequelize = new Sequelize(dbconf);
var _ = Sequelize.Utils._;
/**
* insert models to global
*/
var models = require('../server/models');
_(models).each((model, key) => {
global[key] = new model(sequelize, Sequelize);
});
/**
* create API client
*/
global.client = new (require('./lib/api-client'))('localhost', 3000);
/**
* show info
*/
console.log(`ENV : ${env}`);
console.log(`DB : ${JSON.stringify(dbconf)}`);
var modelName = [];
_(models).each((models, key) => modelName.push(key));
console.log(`MODEL : [${modelName.join(', ')}]`);
console.log(`CLIENT : ${global.client.getHost()}, ${global.client.getPort()}`);
console.log('-----');
var replServer = repl.start({ prompt: 'loq> ' });
@nukosuke

This comment has been minimized.

Copy link
Owner Author

nukosuke commented May 30, 2016

ディレクトリは

/bin/
  |- lib/api-client.js
  |- console

という構成を想定

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.