forked from JeffBuss/jet-fuel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
110 lines (97 loc) · 2.87 KB
/
server.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const fs = require('fs')
const environment = process.env.NODE_ENV || 'development'
const configuration = require('./knexfile')[environment]
const database = require('knex')(configuration)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static('build'))
app.set('port', process.env.PORT || 3000)
app.locals.title = 'Jet Fuel'
app.locals.folders = []
app.locals.urls = []
app.get('/', (request, response) => {
fs.readFile(`${__dirname}/index.html`, (err, file) => {
response.send(file)
})
})
app.get('/:id', (request, response) => {
const id = request.params.id
database('urls').where('id', request.params.id).increment('clicks', 1)
.then(() => {
database('urls').where('id', id).select()
.then((url) => {
response.redirect(`http://${url[0].urlName}`);
})
.catch((error) => {
})
})
})
app.get('/api/folders', (request, response) => {
database('folders').select()
.then((folders) => {
response.status(200).json(folders)
})
.catch((error) => {
})
})
app.post('/api/folders', (request, response) => {
const folderName = request.body.folderName
const folders = { folderName }
database('folders').insert(folders)
.then((folders) => {
database('folders').select()
.then((folders) => {
response.status(200).json(folders)
})
.catch((error) => {
console.log('something is wrong w db (folders)')
})
})
})
app.get('/api/folders/:folderId/urls', (request, response) => {
const { folderId } = request.params
database('urls').where('folderId', folderId).select()
.then((urls) => {
response.status(200).json(urls)
})
.catch((error) => {
console.error('something wrong w db (get urls)')
})
})
app.post('/api/folders/:folderId/urls', (request, response) => {
const { folderId } = request.params
const urlName = request.body.urlName
const d = new Date()
const date = d.toString()
const clicks = 0
const urls = {folderId, urlName, date, clicks}
database('urls').insert(urls)
.then(() => {
database('urls').select()
.then((urls) => {
response.status(200).json(urls)
})
.catch((error) => {
console.log('something is wrong w db (urls)')
})
})
})
app.patch('/api/folders/:folderId/urls', (request, response) => {
database('urls').increment('clicks', 1).where('id', request.body.urlId)
.then(clicks => {
database('urls').where('folderId', request.params.folderId).select()
.then(urls => {
response.status(200).json(urls);
})
.catch(error => {
console.error('somethings wrong with db (patch)')
})
})
})
app.listen(app.get('port'), () => {
console.log(`${app.locals.title} is running on ${app.get('port')}.`)
})
module.exports = app;