Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ First of all, set up your project by creating `.env` file with next content:

```env
mongoUri = "<YOUR MONGO URI>"
jwtSecret = "<SECRET KEY>"
```

Main available scripts in the project directory:
Expand Down Expand Up @@ -59,6 +60,7 @@ module.exports = {
NODE_ENV: "production",
mongoUri: "<YOUR MONGO URI>" /*replace this*/,
httpsRedirect: false /*true if enable*/,
jwtSecret: "<SECRET KEY>",
},
},
],
Expand All @@ -74,4 +76,5 @@ module.exports = {
- NODE_ENV: production
- mongoUri: YOUR MONGO URI
- httpsRedirect: true
- jwtSecret: SECRET KEY
- Click "Deploy branch" button
49 changes: 24 additions & 25 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,16 @@ const PORT = process.env.PORT || 5000
const mongoUri = process.env.mongoUri
const httpsRedirect = process.env.httpsRedirect || false

//temporary backend url
const phpBaseUrl = 'https://php-server-notes.herokuapp.com/'

const app = express()

app.use(express.json({ extended: true }))

//app.use('/api/auth', require('./routes/auth.routes'))
app.use('/api/auth', require('./routes/auth.routes'))
app.use('/api/server', require('./routes/phpserver.routes'))

app.post('/server', function (req, res) {
//console.log("backend redirect", req.url)
res.redirect(307, phpBaseUrl)
})
if (httpsRedirect) app.use(httpToHttps)

if (!devMode) {
if (httpsRedirect) app.use(httpToHttps)
app.use('/', express.static(path.join(__dirname, 'client', 'build')))
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
Expand All @@ -38,16 +32,8 @@ if (!devMode) {

async function start() {
try {
if (mongoUri) {
await mongoose.connect(mongoUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
})
} else {
console.log("\n!!!NO MONGO URI!!!")
}
app.listen(PORT, () => logServerStart(PORT))
connectMongo(mongoUri)
app.listen(PORT, logServerStart)
} catch (e) {
console.log('Server Error', e.message)
process.exit(1)
Expand All @@ -56,12 +42,25 @@ async function start() {

start()

function logServerStart(PORT) {
dns.lookup(os.hostname(), (err, address, fam) => {
const [logN, bef, af] = devMode ? ['Express server', ' ', ':'] : ['React Notes App', '-', '']
console.log(`\n${logN} has been started`)
console.log(`${bef} Local${af} http://localhost:${PORT}`)
console.log(`${bef} On Your Network${af} http://${address}:${PORT}`)
async function connectMongo(mongoUri) {
if (mongoUri) {
await mongoose.connect(mongoUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
})
} else {
console.log("\n!!!NO MONGO URI!!!")
}
}

function logServerStart() {
dns.lookup(os.hostname(), (err, address) => {
const [logName, sBef, sAft] = devMode ? ['Express server', ' ', ':'] : ['React Notes App', '-', '']
console.log(`\n${logName} has been started`)
console.log(`${sBef} Local${sAft} http://localhost:${PORT}`)
console.log(`${sBef} On Your Network${sAft} http://${address}:${PORT}`)
if (err) console.log(err)
})
}

Expand Down
83 changes: 43 additions & 40 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.12.0",
"@testing-library/react": "^11.2.6",
"@testing-library/jest-dom": "^5.13.0",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
"bootstrap": "^4.6.0",
"bootstrap-icons": "^1.4.1",
"bootstrap-icons": "^1.5.0",
"jquery": "^3.6.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-markdown": "^6.0.1",
"react-markdown": "^6.0.2",
"react-scripts": "4.0.3",
"react-stack-grid": "^0.7.1",
"react-textarea-autosize": "^8.3.2",
"react-textarea-autosize": "^8.3.3",
"remark-gfm": "^1.0.0",
"web-vitals": "^0.2.4",
"workbox-background-sync": "^5.1.4",
Expand Down
2 changes: 1 addition & 1 deletion client/src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import logo from './logo.svg';
import logo from './Shared/Logo/logo.svg';
import './App.css';
import CardList from './Cards/CardList'
import AddCard from './Cards/AddCard'
Expand Down
13 changes: 6 additions & 7 deletions client/src/Services/DataService.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import Card, { checkCardsArr } from '../Cards/cardType/Card'
export default function DataService() {
////////////////////////////////////////////////////////////
var user = null
const nodeBackend = true
const baseUrl = nodeBackend ? '/server/' : 'http://php-server-notes.std-1033.ist.mospolytech.ru/'
const baseUrl = '/api/server/'

////////////////////////////////////////////////////////////
function updDataServLogin(login) {
Expand Down Expand Up @@ -74,7 +73,7 @@ export default function DataService() {
function checkData(data) {
//console.log('start check data')
try {
if( data === null) console.log("null data");
if (data === null) console.log("null data");
return data === null || data === [] || checkCardsArr(data)
} catch {
return false
Expand All @@ -93,7 +92,7 @@ export default function DataService() {
.then((d) => {
let data = tryParce(d)//here we parce json
//console.log("[DATA] from loadData(): ", data)
if(!data)console.log("empty data from server");
if (!data) console.log("empty data from server");
if (!checkData(data)) {
console.error("[loadData] Bad data format")
console.log(data)
Expand Down Expand Up @@ -121,10 +120,10 @@ export default function DataService() {
? Promise.reject(rej())
: loadData())
.then((d) => {
if(!data)console.log("empty data to post");
if(!d)console.log("empty loaded to check");
if (!data) console.log("empty data to post");
if (!d) console.log("empty loaded to check");
let pDat = data === null ? (d || []) : data
if(!pDat)console.log("empty will be posted");
if (!pDat) console.log("empty will be posted");
requestPostData(pDat).then(res, rej)
})
.catch(rej)
Expand Down
File renamed without changes
24 changes: 24 additions & 0 deletions middleware/auth.middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const jwt = require('jsonwebtoken')
require('dotenv').config()

module.exports = (req, res, next) => {
if (req.method === 'OPTIONS') {
return next()
}

try {

const token = req.headers.authorization.split(' ')[1] // "Bearer TOKEN"

if (!token) {
return res.status(401).json({ message: 'Нет авторизации' })
}

const decoded = jwt.verify(token, process.env.jwtSecret)
req.user = decoded
next()

} catch (e) {
res.status(401).json({ message: 'Нет авторизации' })
}
}
8 changes: 8 additions & 0 deletions models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const {Schema, model, Types} = require('mongoose')

const schema = new Schema({
email: {type: String, required: true, unique: true},
password: {type: String, required: true}
})

module.exports = model('User', schema)
Loading