김연준

remote mongodb access in ec2

1 +npm-debug.log
...\ No newline at end of file ...\ No newline at end of file
...@@ -3,25 +3,25 @@ const jokedoc = require('../joke_data/jokes.json'); ...@@ -3,25 +3,25 @@ const jokedoc = require('../joke_data/jokes.json');
3 const dbname = 'jokeapi'; 3 const dbname = 'jokeapi';
4 const collec = 'jokes'; 4 const collec = 'jokes';
5 const dboperation = require('./operations.js'); 5 const dboperation = require('./operations.js');
6 -var url = "mongodb://localhost:27017/jokeapi"; 6 +var url = "mongodb://13.124.65.242:27017/jokeapi";
7 - 7 +
8 -//Create database 8 +// //Create database
9 -MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { 9 +// MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
10 - if (err) throw err; 10 +// if (err) throw err;
11 - console.log("Database created!"); 11 +// console.log("Database created!");
12 - db.close(); 12 +// db.close();
13 -}); 13 +// });
14 - 14 +
15 -//Create collection 15 +// //Create collection
16 -MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { 16 +// MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
17 - if (err) throw err; 17 +// if (err) throw err;
18 - var dbo = db.db("jokeapi"); 18 +// var dbo = db.db("jokeapi");
19 - dbo.createCollection("jokes", function(err, res) { 19 +// dbo.createCollection("jokes", function(err, res) {
20 - if (err) throw err; 20 +// if (err) throw err;
21 - console.log("Collection created!"); 21 +// console.log("Collection created!");
22 - db.close(); 22 +// db.close();
23 - }); 23 +// });
24 - }); 24 +// });
25 25
26 26
27 //Storing data jokes.json data into mongodb 27 //Storing data jokes.json data into mongodb
......
...@@ -3,7 +3,7 @@ const jokedoc = require('../joke_data/Funny_story.json'); ...@@ -3,7 +3,7 @@ const jokedoc = require('../joke_data/Funny_story.json');
3 const dbname = 'FunnyStoryapi'; 3 const dbname = 'FunnyStoryapi';
4 const collec = 'FunnyStory'; 4 const collec = 'FunnyStory';
5 const dboperation = require('./operations.js'); 5 const dboperation = require('./operations.js');
6 -var url = "mongodb://localhost:27017/FunnyStoryapi"; 6 +var url = "mongodb://13.124.65.242:27017/FunnyStoryapi";
7 7
8 //Create database 8 //Create database
9 MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { 9 MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
...@@ -16,7 +16,7 @@ MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { ...@@ -16,7 +16,7 @@ MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
16 MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { 16 MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
17 if (err) throw err; 17 if (err) throw err;
18 var dbo = db.db("FunnyStoryapi"); 18 var dbo = db.db("FunnyStoryapi");
19 - dbo.createCollection("FunnyStory", function(err, res) { 19 + dbo.createCollection("FunnyStories", function(err, res) {
20 if (err) throw err; 20 if (err) throw err;
21 console.log("Collection created!"); 21 console.log("Collection created!");
22 db.close(); 22 db.close();
......
...@@ -3,7 +3,7 @@ const jokedoc = require('../joke_data/reddit_joke.json'); ...@@ -3,7 +3,7 @@ const jokedoc = require('../joke_data/reddit_joke.json');
3 const dbname = 'redditjoke'; 3 const dbname = 'redditjoke';
4 const collec = 'reddit'; 4 const collec = 'reddit';
5 const dboperation = require('./operations.js'); 5 const dboperation = require('./operations.js');
6 -var url = "mongodb://localhost:27017/redditjoke"; 6 +var url = "mongodb://13.124.65.242:27017/redditjoke";
7 7
8 //Create database 8 //Create database
9 MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { 9 MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
...@@ -15,7 +15,7 @@ MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { ...@@ -15,7 +15,7 @@ MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
15 //Create collection 15 //Create collection
16 MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { 16 MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
17 if (err) throw err; 17 if (err) throw err;
18 - var dbo = db.db("reddit"); 18 + var dbo = db.db("redditjoke");
19 dbo.createCollection("redditjokes", function(err, res) { 19 dbo.createCollection("redditjokes", function(err, res) {
20 if (err) throw err; 20 if (err) throw err;
21 console.log("Collection created!"); 21 console.log("Collection created!");
......
...@@ -3,7 +3,7 @@ const jokedoc = require('../joke_data/user.json'); ...@@ -3,7 +3,7 @@ const jokedoc = require('../joke_data/user.json');
3 const dbname = 'userdb'; 3 const dbname = 'userdb';
4 const collec = 'user'; 4 const collec = 'user';
5 const dboperation = require('./operations.js'); 5 const dboperation = require('./operations.js');
6 -var url = "mongodb://localhost:27017/userdb"; 6 +var url = "mongodb://13.124.65.242:27017/userdb";
7 7
8 //Create database 8 //Create database
9 MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { 9 MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
......
...@@ -2,16 +2,16 @@ const MongoClient = require('mongodb').MongoClient; ...@@ -2,16 +2,16 @@ const MongoClient = require('mongodb').MongoClient;
2 const SlackBot = require('slackbots'); //link : https://github.com/mishk0/slack-bot-api 2 const SlackBot = require('slackbots'); //link : https://github.com/mishk0/slack-bot-api
3 const dbname = 'jokeapi'; 3 const dbname = 'jokeapi';
4 const emoji = require('../slack_emoji'); 4 const emoji = require('../slack_emoji');
5 -const url = 'mongodb://localhost:27017/'; 5 +const url = 'mongodb://13.124.65.242:27017/';
6 -var url2 = 'mongodb://localhost:27017/userjoke' 6 +const url2 = 'mongodb://13.124.65.242:27017/userdb';
7 -const fs = require('fs'); 7 +const fs=require('fs');
8 -//const userjoke = require('./joke_data/user.json');
9 message_recieved = 0; 8 message_recieved = 0;
9 +
10 exports.startbot = ()=>{ 10 exports.startbot = ()=>{
11 // Get authorization to use the slackbot 11 // Get authorization to use the slackbot
12 const bot = new SlackBot({ 12 const bot = new SlackBot({
13 - token : "xoxb-651692943605-645515979745-VInZkUepi88jyzhpps44uRyM", 13 + token : "xoxb-582582124755-587875604934-gdWsVhzGXCze3mECWXpTsAP6",
14 - name : "jokebot" 14 + name : "joker"
15 }); 15 });
16 16
17 // Start the slackbot 17 // Start the slackbot
...@@ -111,7 +111,7 @@ function handleMessage(message, current_channel){ ...@@ -111,7 +111,7 @@ function handleMessage(message, current_channel){
111 UserMakeJoke(message,current_channel); 111 UserMakeJoke(message,current_channel);
112 } 112 }
113 else if(message.includes(' me')){ 113 else if(message.includes(' me')){
114 - comment = "Please use @joker [-help] to know what I can do!:smile::smile::smile:\n You can write type of joke[knock-knock, general, programming, funny story, reddit]"; 114 + comment = "Please use @joker --help to know what I can do!:smile::smile::smile:\n You can write type of joke[knock-knock, general, programming, funny story, reddit]";
115 bot.postMessageToChannel(current_channel, "Tell you what??? :no_mouth:", emoji.emojis('no_mouth')); 115 bot.postMessageToChannel(current_channel, "Tell you what??? :no_mouth:", emoji.emojis('no_mouth'));
116 bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed')); 116 bot.postMessageToChannel(current_channel, comment, emoji.emojis('flushed'));
117 } 117 }
...@@ -156,7 +156,7 @@ function MakeJoke(message,user_channel){ ...@@ -156,7 +156,7 @@ function MakeJoke(message,user_channel){
156 var length=obj.table.length; 156 var length=obj.table.length;
157 obj.table.push({id : length+1, type : user, setup : msg[0], punchline : msg[1]}); 157 obj.table.push({id : length+1, type : user, setup : msg[0], punchline : msg[1]});
158 var myobj = {id : length+1, type : user, setup : msg[0], punchline : msg[1]}; 158 var myobj = {id : length+1, type : user, setup : msg[0], punchline : msg[1]};
159 - MongoClient.connect(url2,function(err,db){ 159 + MongoClient.connect(url,function(err,db){
160 if(err) throw err; 160 if(err) throw err;
161 var dbo = db.db("userdb"); 161 var dbo = db.db("userdb");
162 dbo.collection("user").insertOne(myobj, function(err,res){ 162 dbo.collection("user").insertOne(myobj, function(err,res){
...@@ -164,7 +164,6 @@ function MakeJoke(message,user_channel){ ...@@ -164,7 +164,6 @@ function MakeJoke(message,user_channel){
164 console.log("1 insert!!"); 164 console.log("1 insert!!");
165 db.close; 165 db.close;
166 }) 166 })
167 -
168 }) 167 })
169 168
170 var json=JSON.stringify(obj); 169 var json=JSON.stringify(obj);
...@@ -183,24 +182,8 @@ function MakeJoke(message,user_channel){ ...@@ -183,24 +182,8 @@ function MakeJoke(message,user_channel){
183 console.log("file not exists"); 182 console.log("file not exists");
184 obj.table.push({id : 1, type : user, setup : msg[0], punchline : msg[1]}); 183 obj.table.push({id : 1, type : user, setup : msg[0], punchline : msg[1]});
185 var myobj = {id : 1, type : user, setup : msg[0], punchline : msg[1]}; 184 var myobj = {id : 1, type : user, setup : msg[0], punchline : msg[1]};
186 - //Create database 185 +
187 - MongoClient.connect(url2, { useNewUrlParser: true }, function(err, db) { 186 + MongoClient.connect(url,{ useNewUrlParser: true },function(err,db){
188 - if (err) throw err;
189 - console.log("Database created!");
190 - db.close();
191 - });
192 -
193 - //Create collection
194 - MongoClient.connect(url2, { useNewUrlParser: true }, function(err, db) {
195 - if (err) throw err;
196 - var dbo = db.db("userdb");
197 - dbo.createCollection("user", function(err, res) {
198 - if (err) throw err;
199 - console.log("Collection created!");
200 - db.close();
201 - });
202 - });
203 - MongoClient.connect(url2,{ useNewUrlParser: true },function(err,db){
204 if(err) throw err; 187 if(err) throw err;
205 var dbo = db.db("userdb"); 188 var dbo = db.db("userdb");
206 dbo.collection("user").insertOne(myobj, function(err,res){ 189 dbo.collection("user").insertOne(myobj, function(err,res){
...@@ -208,7 +191,6 @@ function MakeJoke(message,user_channel){ ...@@ -208,7 +191,6 @@ function MakeJoke(message,user_channel){
208 console.log("1 insert!!"); 191 console.log("1 insert!!");
209 db.close; 192 db.close;
210 }) 193 })
211 -
212 }); 194 });
213 var json=JSON.stringify(obj); 195 var json=JSON.stringify(obj);
214 fs.writeFile(path,json,'utf8',function(err){ 196 fs.writeFile(path,json,'utf8',function(err){
...@@ -235,7 +217,7 @@ function getRandomInt(start,end) { ...@@ -235,7 +217,7 @@ function getRandomInt(start,end) {
235 //Function for giving out random joke 217 //Function for giving out random joke
236 randomJoke= (user_channel)=>{ 218 randomJoke= (user_channel)=>{
237 //Connect to mongodb client 219 //Connect to mongodb client
238 - MongoClient.connect('mongodb://localhost:27017', function (err, client){ 220 + MongoClient.connect('mongodb://13.124.65.242:27017/', function (err, client){
239 if (err) throw err; 221 if (err) throw err;
240 //go into database name jokeapi 222 //go into database name jokeapi
241 var db = client.db('jokeapi'); 223 var db = client.db('jokeapi');
...@@ -272,27 +254,42 @@ UserMakeJoke= (message,user_channel)=>{ ...@@ -272,27 +254,42 @@ UserMakeJoke= (message,user_channel)=>{
272 var temp=message.split(' '); 254 var temp=message.split(' ');
273 var user=temp[0].substring(2,temp[0].length-1); 255 var user=temp[0].substring(2,temp[0].length-1);
274 console.log('유저 => '+user); 256 console.log('유저 => '+user);
257 +
258 + MongoClient.connect('mongodb://13.124.65.242:27017', function (err, client){
259 + if (err) throw err;
260 + //go into database name jokeapi
261 + var db = client.db('userdb');
262 +
263 + result = db.collection('user').findOne({type: user});
264 + console.log(result);
265 +
266 + user = result;
267 + //After finding one joke, use promise to run codes synchronously
268 + user.then(function(total){
269 + question = total.setup;
270 + bot.postMessageToChannel(user_channel, question, emoji.emojis('laughing'));
271 + console.log("질문 불려짐");
272 + return total;
273 + })
274 + .then((all)=>{
275 + joke=all.punchline;
276 + //Use setTimeout function to delay the code execution, making sure the user reads the question first and then see the final funny joke
277 + setTimeout(function secondFunction(){
278 + bot.postMessageToChannel(user_channel,`${joke}:stuck_out_tongue_winking_eye::laughing:`,emoji.emojis('laughing'));
279 + console.log("허무개그 전송~~~~~~!");
280 + },3000);
281 + })
282 + //close mongodb
283 + client.close();
284 + })
285 +
275 286
276 var path='./joke_data/'+user+'.json'; 287 var path='./joke_data/'+user+'.json';
277 288
278 var data=fs.readFileSync(path); 289 var data=fs.readFileSync(path);
279 var jsondata=JSON.parse(data); 290 var jsondata=JSON.parse(data);
280 - random=getRandomInt(1,jsondata.table.length+1);
281 291
282 - console.log(random); 292 +
283 - for(var i=0;i<jsondata.table.length;i++){
284 - if(jsondata.table[i].id==random){
285 - user=jsondata.table[i];
286 - question=user.setup;
287 - bot.postMessageToChannel(user_channel,question, emoji.emojis('laughing'));
288 - joke=user.punchline;
289 - setTimeout(function secondfunction(){
290 - bot.postMessageToChannel(user_channel,joke, emoji.emojis('laughing'));
291 - },3000);
292 - console.log("User joke~~~");
293 - break;
294 - }
295 - }
296 } 293 }
297 294
298 //Function for giving out random joke after filtering only general type jokes 295 //Function for giving out random joke after filtering only general type jokes
...@@ -366,7 +363,7 @@ Funnystory= (user_channel)=>{ ...@@ -366,7 +363,7 @@ Funnystory= (user_channel)=>{
366 cate = total.category; 363 cate = total.category;
367 story = total.body; 364 story = total.body;
368 category_story=cate+'\n'+story+':stuck_out_tongue_winking_eye::laughing:'; 365 category_story=cate+'\n'+story+':stuck_out_tongue_winking_eye::laughing:';
369 - bot.postMessageToChannel(user_channel, category_story, emoji.emojis('smiliey')); 366 + bot.postMessageToChannel(user_channel, category_story, emoji.emojis('smiley'));
370 console.log("Funny story~~~"); 367 console.log("Funny story~~~");
371 }) 368 })
372 client.close(); 369 client.close();
...@@ -387,7 +384,7 @@ redditJoke= (user_channel)=>{ ...@@ -387,7 +384,7 @@ redditJoke= (user_channel)=>{
387 user.then(function(total){ 384 user.then(function(total){
388 title = total.title; 385 title = total.title;
389 bot.postMessageToChannel(user_channel, title, emoji.emojis('smiliey')); 386 bot.postMessageToChannel(user_channel, title, emoji.emojis('smiliey'));
390 - console.log("quiz~"); 387 + console.log("질문 불려짐");
391 return total; 388 return total;
392 }) 389 })
393 .then((all)=>{ 390 .then((all)=>{
...@@ -395,7 +392,7 @@ redditJoke= (user_channel)=>{ ...@@ -395,7 +392,7 @@ redditJoke= (user_channel)=>{
395 //Use setTimeout function to delay the code execution, making sure the user reads the question first and then see the final funny joke 392 //Use setTimeout function to delay the code execution, making sure the user reads the question first and then see the final funny joke
396 setTimeout(function secondFunction(){ 393 setTimeout(function secondFunction(){
397 bot.postMessageToChannel(user_channel,`${joke}:stuck_out_tongue_winking_eye::laughing:`,emoji.emojis('laughing')); 394 bot.postMessageToChannel(user_channel,`${joke}:stuck_out_tongue_winking_eye::laughing:`,emoji.emojis('laughing'));
398 - console.log("reddit joke~~~"); 395 + console.log("허무개그 전송~~~~~~!");
399 },3000); 396 },3000);
400 }) 397 })
401 client.close(); 398 client.close();
......
1 -{"table":[{"id":1,"type":"UJZF5UTMX","setup":" ok","punchline":" okkk"},{"id":2,"type":"UJZF5UTMX","setup":" man","punchline":" woman"},{"id":3,"type":"UJZF5UTMX","setup":" kim ","punchline":" minuk"}]}
...\ No newline at end of file ...\ No newline at end of file
1 +{"table":[{"id":1,"type":"UJZF5UTMX","setup":" ok","punchline":" okkk"},{"id":2,"type":"UJZF5UTMX","setup":" man","punchline":" woman"},{"id":3,"type":"UJZF5UTMX","setup":" kim ","punchline":" minuk"},{"id":4,"type":"UH9RRHSTG","setup":" 팔에 비빔면 뭍으면 빨리 닦아야됨 왜?","punchline":" 팔도비빔면됨"},{"id":5,"type":"UH9RRHSTG","setup":" 팔에 비빔소스 묻으면 빨리 닦아야함","punchline":" 팔도비빔면되서"},{"id":6,"type":"UH9RRHSTG","setup":" check","punchline":" working"}]}
...\ No newline at end of file ...\ No newline at end of file
......
1 +0 info it worked if it ends with ok
2 +1 verbose cli [ '/usr/bin/node', '/usr/bin/npm', 'start' ]
3 +2 info using npm@3.5.2
4 +3 info using node@v8.10.0
5 +4 verbose run-script [ 'prestart', 'start', 'poststart' ]
6 +5 info lifecycle joker@1.0.0~prestart: joker@1.0.0
7 +6 silly lifecycle joker@1.0.0~prestart: no script for prestart, continuing
8 +7 info lifecycle joker@1.0.0~start: joker@1.0.0
9 +8 verbose lifecycle joker@1.0.0~start: unsafe-perm in lifecycle true
10 +9 verbose lifecycle joker@1.0.0~start: PATH: /usr/share/npm/bin/node-gyp-bin:/home/kyjyeon/Desktop/khuhub/TeamProject/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
11 +10 verbose lifecycle joker@1.0.0~start: CWD: /home/kyjyeon/Desktop/khuhub/TeamProject
12 +11 silly lifecycle joker@1.0.0~start: Args: [ '-c', 'node index.js' ]
13 +12 silly lifecycle joker@1.0.0~start: Returned: code: 1 signal: null
14 +13 info lifecycle joker@1.0.0~start: Failed to exec start script
15 +14 verbose stack Error: joker@1.0.0 start: `node index.js`
16 +14 verbose stack Exit status 1
17 +14 verbose stack at EventEmitter.<anonymous> (/usr/share/npm/lib/utils/lifecycle.js:232:16)
18 +14 verbose stack at emitTwo (events.js:126:13)
19 +14 verbose stack at EventEmitter.emit (events.js:214:7)
20 +14 verbose stack at ChildProcess.<anonymous> (/usr/share/npm/lib/utils/spawn.js:24:14)
21 +14 verbose stack at emitTwo (events.js:126:13)
22 +14 verbose stack at ChildProcess.emit (events.js:214:7)
23 +14 verbose stack at maybeClose (internal/child_process.js:925:16)
24 +14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
25 +15 verbose pkgid joker@1.0.0
26 +16 verbose cwd /home/kyjyeon/Desktop/khuhub/TeamProject
27 +17 error Linux 4.18.0-15-generic
28 +18 error argv "/usr/bin/node" "/usr/bin/npm" "start"
29 +19 error node v8.10.0
30 +20 error npm v3.5.2
31 +21 error code ELIFECYCLE
32 +22 error joker@1.0.0 start: `node index.js`
33 +22 error Exit status 1
34 +23 error Failed at the joker@1.0.0 start script 'node index.js'.
35 +23 error Make sure you have the latest version of node.js and npm installed.
36 +23 error If you do, this is most likely a problem with the joker package,
37 +23 error not with npm itself.
38 +23 error Tell the author that this fails on your system:
39 +23 error node index.js
40 +23 error You can get information on how to open an issue for this project with:
41 +23 error npm bugs joker
42 +23 error Or if that isn't available, you can get their info via:
43 +23 error npm owner ls joker
44 +23 error There is likely additional logging output above.
45 +24 verbose exit [ 1, true ]