Fix axios and server setup

This commit is contained in:
Kevin Thomas
2021-08-08 23:52:44 -07:00
parent cd6af207be
commit ad810beea9
15 changed files with 17131 additions and 110 deletions

View File

@@ -3,11 +3,8 @@
"listeningPort": 3000,
"sessionFileStorePath": "./sessions",
"sessionSecret": "cats",
"tls": true,
"tls": false,
"tlsKey": "./certs/key.pem",
"tlsCert": "./certs/cert.pem"
},
"Client": {
"listeningPort": 8080
}
}

17153
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -5,6 +5,7 @@
"scripts": {
"serve": "vue-cli-service serve",
"server": "node server/bin/www.js",
"server-prod": "NODE_ENV=production node server/bin/www.js",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
@@ -13,11 +14,11 @@
"config": "^3.3.6",
"cookie-parser": "^1.4.5",
"core-js": "^3.6.5",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-session": "^1.17.2",
"passport": "^0.4.1",
"passport-local": "^1.0.0",
"path": "^0.12.7",
"session-file-store": "^1.5.0",
"sqlite3": "^5.0.2",
"tone": "^14.7.77",

View File

@@ -1,18 +1,14 @@
const express = require('express')
const session = require('express-session')
const FileStore = require('session-file-store')(session)
const cors = require('cors')
const passport = require('passport')
const path = require('path')
const cookieParser = require('cookie-parser')
const config = require('config')
const authRouter = require('./routes/auth')
const usersRouter = require('./routes/users')
const profilesRouter = require('./routes/profiles')
const app = express()
const corsOptions = {
origin: 'http://localhost:'.concat(config.get('Client.listeningPort')),
credentials: true
}
const fileStoreOptions = {
path: config.get('Server.sessionFileStorePath')
}
@@ -20,10 +16,12 @@ const fileStoreOptions = {
require('./boot/db')()
require('./boot/auth')()
app.use(cors(corsOptions))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, '../dist')))
}
app.use(session({
store: new FileStore(fileStoreOptions),
secret: config.get('Server.sessionSecret'),

View File

@@ -1,8 +1,5 @@
#!/usr/bin/env node
/**
* Module dependencies.
*/
const app = require('../app')
const debug = require('debug')('example:server')
const fs = require('fs')
@@ -10,9 +7,6 @@ const config = require('config')
const tls = config.get('Server.tls')
const http = require(tls ? 'https' : 'http')
/**
* Get port from environment and store in Express.
*/
const port = normalizePort(config.get('Server.listeningPort'))
app.set('port', port)
@@ -26,16 +20,10 @@ if (tls) {
server = http.createServer(httpsOptions, app)
}
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port)
server.on('error', onError)
server.on('listening', onListening)
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort (val) {
const port = parseInt(val, 10)
@@ -52,9 +40,6 @@ function normalizePort (val) {
return false
}
/**
* Event listener for HTTP server "error" event.
*/
function onError (error) {
if (error.syscall !== 'listen') {
throw error
@@ -77,9 +62,6 @@ function onError (error) {
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening () {
const addr = server.address()
const bind = typeof addr === 'string'

View File

@@ -98,7 +98,7 @@ router.patch('/users/:userId', function (req, res) {
return res.sendStatus(401)
}
db.run('UPDATE users SET is_admin = ? WHERE id = ?', [req.body.isAdmin, req.params.userId], (err) => {
db.run('UPDATE users SET is_admin = ? WHERE id = ?', [req.body.isAdmin ? 1 : 0, req.params.userId], (err) => {
if (err) {
return res.sendStatus(500)
}

7
src/axios.js Normal file
View File

@@ -0,0 +1,7 @@
import Axios from 'axios'
const instance = Axios.create({
withCredentials: true
})
export default instance

View File

@@ -82,7 +82,7 @@ export default {
},
methods: {
getUsers () {
this.$http.get('https://localhost:3000/users')
this.$http.get('/users')
.then(response => {
if (response.status === 200) {
this.users = response.data.users
@@ -93,8 +93,8 @@ export default {
})
},
updateUser (id, isAdmin) {
this.$http.patch('https://localhost:3000/users/'.concat(id), {
isAdmin: isAdmin ? 1 : 0
this.$http.patch('/users/'.concat(id), {
isAdmin: isAdmin
})
.then(response => {
if (response.status === 200) {
@@ -107,7 +107,7 @@ export default {
})
},
deleteUser (id) {
this.$http.delete('https://localhost:3000/users/'.concat(id))
this.$http.delete('/users/'.concat(id))
.then(response => {
if (response.status === 200) {
this.getUsers()

View File

@@ -81,7 +81,7 @@ export default {
this.$router.push('/admin')
},
logout () {
this.$http.get('https://localhost:3000/logout')
this.$http.get('/logout')
.then(response => {
if (response.status === 200) {
this.$router.push('/login')
@@ -92,7 +92,7 @@ export default {
})
},
openDrawyer () {
this.$http.get('https://localhost:3000/users/current')
this.$http.get('/users/current')
.then(response => {
if (response.data.user.isAdmin) {
this.isAdmin = true

View File

@@ -60,7 +60,7 @@ export default {
}),
methods: {
login () {
this.$http.post('https://localhost:3000/login/password', {
this.$http.post('/login/password', {
username: this.username,
password: this.password
})

View File

@@ -70,7 +70,7 @@ export default {
}),
methods: {
register () {
this.$http.post('https://localhost:3000/users', {
this.$http.post('/users', {
name: this.name,
username: this.username,
password: this.password,

View File

@@ -133,7 +133,7 @@ export default {
}
},
populateProfileItems () {
this.$http.get('https://localhost:3000/profiles')
this.$http.get('/profiles')
.then(response => {
if (response.status === 200) {
this.profileItems = response.data.profiles
@@ -144,7 +144,7 @@ export default {
})
},
saveProfile () {
this.$http.post('https://localhost:3000/profiles', {
this.$http.post('/profiles', {
name: this.profileName,
isTimerEnabled: this.isTimerEnabled,
duration: this.duration,
@@ -169,7 +169,7 @@ export default {
this.populateProfileItems()
},
loadProfile () {
this.$http.get('https://localhost:3000/profiles/'.concat(this.selectedProfile.id))
this.$http.get('/profiles/'.concat(this.selectedProfile.id))
.then(response => {
if (response.status === 200) {
const profile = response.data.profile
@@ -195,7 +195,7 @@ export default {
})
},
deleteProfile () {
this.$http.delete('https://localhost:3000/profiles/'.concat(this.selectedProfile.id))
this.$http.delete('/profiles/'.concat(this.selectedProfile.id))
.then(response => {
if (response.status === 200) {
this.populateProfileItems()

View File

@@ -2,12 +2,7 @@ import Vue from 'vue'
import App from './App.vue'
import router from './router'
import vuetify from './plugins/vuetify'
import Axios from 'axios'
const instance = Axios.create({
baseURL: 'https://localhost:3000',
withCredentials: true
})
import instance from './axios'
Vue.prototype.$http = instance

View File

@@ -1,12 +1,7 @@
import Vue from 'vue'
import Axios from 'axios'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
const instance = Axios.create({
baseURL: 'https://localhost:3000',
withCredentials: true
})
import instance from '../axios'
Vue.use(VueRouter)

View File

@@ -1,5 +1,8 @@
module.exports = {
transpileDependencies: [
'vuetify'
]
],
devServer: {
proxy: 'http://localhost:3000'
}
}