-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (88 loc) · 2.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//install serve-static a middleware to bypass expresss default look for index.html file in folders that are served ex:public
const express= require('express');
const serveStatic = require('serve-static');
require('dotenv').config();
const datastore= require('nedb');
//const bodyparser= require('body-parser');
const fetch= require('node-fetch');
const app= express();
app.listen(3005,()=>{console.log("listening on :http://localhost:3005")});
app.use(serveStatic('public',{'index':['client.html']}));
const database=new datastore('database.db');
database.loadDatabase();
app.use(express.json());
app.get('/learn',(req,res)=>{
database.find({},(err,data)=>{
res.json(data);
})
});
app.post('/word',(req,res)=>{
let date=new Date();
let currentdate= new Date();
date = date.setDate(date.getDate() + req.body.totalcount);
//let fd=new Date(Number(d));
const data={
word:req.body.word,
pronounciation:req.body.pronounciation,
meaning:req.body.meaning,
examples:req.body.examples,
description:req.body.description,
audio:req.body.audio,
date:date,
created:currentdate.toString(),
};
database.insert(data);
res.json(data);
});
app.post('/change',(req,res)=>{
let date= new Date();
date = date.setDate(date.getDate()+req.body.count);
//let fd= new Date(d);
let currentdate=new Date();
database.remove({word:req.body.w},{});
const data={
word:req.body.w,
meaning:req.body.m,
description:req.body.d,
date:date,
created:currentdate.toString()
}
database.insert(data);
res.json(data);
});
app.post('/delete',(req,res)=>{
database.remove({word:req.body},{});
});
app.get('/test/:word',async (req,res)=>{
const word=req.params.word;
const apikey=process.env.API_KEY;
const wordnik='https://api.wordnik.com/v4/word.json/';
const a_url=wordnik+word+'/audio?useCanonical=true&limit=50&api_key='+apikey;
const m_url=wordnik+word+'/definitions?&limit=3&includeRelated=false&useCanonical=true&includeTags=false&api_key='+apikey;
const e_url=wordnik+word+'/examples?includeDuplicates=false&useCanonical=true&limit=2&api_key='+apikey;
const p_url=wordnik+word+'/pronunciations?useCanonical=true&limit=1&api_key='+apikey;
const audio=await fetch(a_url);
const aresponse=await audio.json();
const meaning=await fetch(m_url);
const mresponse=await meaning.json();
const examples=await fetch(e_url);
const eresponse=await examples.json();
const pronounciation=await fetch(p_url);
const presponse=await pronounciation.json();
const sent={
m:mresponse,
p:presponse,
e:eresponse,
a:aresponse
};
res.json(sent);
});
// app.get('/atest/:word',async (req,res)=>{
// const word=req.params.word;
// const apikey=process.env.API_KEY;
// const wordnik='https://api.wordnik.com/v4/word.json/';
// const a_url=
// const audio= await fetch(a_url);
// const reponse = audio.blob();
// res.blob(reponse);
// });