-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
99 lines (87 loc) · 2.39 KB
/
server.js
File metadata and controls
99 lines (87 loc) · 2.39 KB
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
91
92
93
94
95
96
97
98
99
/* eslint-disable no-console */
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const pgp = require('pg-promise')();
const fetch = require('node-fetch');
const path = require('path');
const bcrypt = require('bcrypt');
const saltRounds = 10;
const app = express();
const db = pgp({
host: 'localhost',
port: 5432,
database: process.env.DB_NAME,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
});
const wordsApi = process.env.WORDS_API;
app.use(express.static(path.join(__dirname, 'dist')));
app.use(bodyParser.json());
function createWordObj(content) {
return Object.assign(
{},
{
word: content.word,
definition: content.results[0].definition,
}
);
}
// fetch from words api
app.post('/api/words', (req, res) => {
const url = `https://wordsapiv1.p.mashape.com/words/?partOfSpeech=${req.body.partofspeech}&random=true`;
fetch(url, {
headers: {
'X-Mashape-Key': `${wordsApi}`,
'X-Mashape-Host': 'wordsapiv1.p.mashape.com',
},
})
.then(response => response.json())
.then(content => res.json(createWordObj(content)))
.catch(console.error);
});
app.post('/api/writer', (req, res) => {
bcrypt
.hash(req.body.registrationPassword, saltRounds)
.then(function(hash) {
return db.one(
`INSERT INTO writer (first_name, email, hash) VALUES ($1,$2,$3) RETURNING id`,
[req.body.firstName, req.body.registrationEmail, hash]
);
})
.then(result => {
return res.json({ id: result.id });
})
.catch(error => res.json({ error: error.message }));
});
app.get('/api/writer', function(req, res) {
db.any('SELECT * FROM writer')
.then(function(data) {
res.json(data);
})
.catch(function(error) {
res.json({ error: error.message });
});
});
app.get('/api/writing', function(req, res) {
db.any('SELECT * FROM writing')
.then(function(data) {
res.json(data);
})
.catch(function(error) {
res.json({ error: error.message });
});
});
// BASE REQUEST
// Handles any requests that don't match the ones above
app.get('*', (req, res) => {
res.sendFile(path.join(`${__dirname}/dist/index.html`), function(err) {
if (err) {
res.status(500).send(err);
}
});
});
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`Listening on port number ${port}`);
});