Skip to content

Commit b862767

Browse files
committed
Added notes-backend for testing
1 parent bf79b63 commit b862767

40 files changed

+11763
-24
lines changed

part4/notes-backend/.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build

part4/notes-backend/.eslintrc.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module.exports = {
2+
'env': {
3+
'commonjs': true,
4+
'es2021': true,
5+
'node': true,
6+
'jest': true,
7+
},
8+
'extends': 'eslint:recommended',
9+
'parserOptions': {
10+
'ecmaVersion': 'latest'
11+
},
12+
'rules': {
13+
'indent': [
14+
'error',
15+
2
16+
],
17+
'linebreak-style': [
18+
'error',
19+
'unix'
20+
],
21+
'quotes': [
22+
'error',
23+
'single'
24+
],
25+
'semi': [
26+
'error',
27+
'never'
28+
],
29+
'eqeqeq': 'error',
30+
'no-trailing-spaces': 'error',
31+
'object-curly-spacing': [
32+
'error', 'always'
33+
],
34+
'arrow-spacing': [
35+
'error', { 'before': true, 'after': true }
36+
],
37+
'no-console': 0,
38+
}
39+
}

part4/notes-backend/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.env

part4/notes-backend/Procfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: npm start

part4/notes-backend/app.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const config = require('./utils/config')
2+
const express = require('express')
3+
const app = express()
4+
const cors = require('cors')
5+
const notesRouter = require('./controllers/notes')
6+
const middleware = require('./utils/middleware')
7+
const logger = require('./utils/logger')
8+
const mongoose = require('mongoose')
9+
10+
logger.info('connecting to', config.MONGODB_URI)
11+
12+
mongoose.connect(config.MONGODB_URI)
13+
.then(() => {
14+
logger.info('connected to MongoDB')
15+
})
16+
.catch((error) => {
17+
logger.error('error connection to MongoDB:', error.message)
18+
})
19+
20+
app.use(cors())
21+
app.use(express.static('build'))
22+
app.use(express.json())
23+
app.use(middleware.requestLogger)
24+
25+
app.use('/api/notes', notesRouter)
26+
27+
app.use(middleware.unknownEndpoint)
28+
app.use(middleware.errorHandler)
29+
30+
module.exports = app
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"files": {
3+
"main.css": "/static/css/main.1becb9f2.css",
4+
"main.js": "/static/js/main.88d3369d.js",
5+
"index.html": "/index.html",
6+
"main.1becb9f2.css.map": "/static/css/main.1becb9f2.css.map",
7+
"main.88d3369d.js.map": "/static/js/main.88d3369d.js.map"
8+
},
9+
"entrypoints": [
10+
"static/css/main.1becb9f2.css",
11+
"static/js/main.88d3369d.js"
12+
]
13+
}

part4/notes-backend/build/favicon.ico

3.78 KB
Binary file not shown.

part4/notes-backend/build/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>React App</title><script defer="defer" src="/static/js/main.88d3369d.js"></script><link href="/static/css/main.1becb9f2.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>

part4/notes-backend/build/logo192.png

5.22 KB
Loading

part4/notes-backend/build/logo512.png

9.44 KB
Loading
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"short_name": "React App",
3+
"name": "Create React App Sample",
4+
"icons": [
5+
{
6+
"src": "favicon.ico",
7+
"sizes": "64x64 32x32 24x24 16x16",
8+
"type": "image/x-icon"
9+
},
10+
{
11+
"src": "logo192.png",
12+
"type": "image/png",
13+
"sizes": "192x192"
14+
},
15+
{
16+
"src": "logo512.png",
17+
"type": "image/png",
18+
"sizes": "512x512"
19+
}
20+
],
21+
"start_url": ".",
22+
"display": "standalone",
23+
"theme_color": "#000000",
24+
"background_color": "#ffffff"
25+
}

part4/notes-backend/build/robots.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# https://www.robotstxt.org/robotstxt.html
2+
User-agent: *
3+
Disallow:

part4/notes-backend/build/static/css/main.1becb9f2.css

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

part4/notes-backend/build/static/css/main.1becb9f2.css.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

part4/notes-backend/build/static/js/main.88d3369d.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
object-assign
3+
(c) Sindre Sorhus
4+
@license MIT
5+
*/
6+
7+
/** @license React v0.20.2
8+
* scheduler.production.min.js
9+
*
10+
* Copyright (c) Facebook, Inc. and its affiliates.
11+
*
12+
* This source code is licensed under the MIT license found in the
13+
* LICENSE file in the root directory of this source tree.
14+
*/
15+
16+
/** @license React v17.0.2
17+
* react-dom.production.min.js
18+
*
19+
* Copyright (c) Facebook, Inc. and its affiliates.
20+
*
21+
* This source code is licensed under the MIT license found in the
22+
* LICENSE file in the root directory of this source tree.
23+
*/
24+
25+
/** @license React v17.0.2
26+
* react-jsx-runtime.production.min.js
27+
*
28+
* Copyright (c) Facebook, Inc. and its affiliates.
29+
*
30+
* This source code is licensed under the MIT license found in the
31+
* LICENSE file in the root directory of this source tree.
32+
*/
33+
34+
/** @license React v17.0.2
35+
* react.production.min.js
36+
*
37+
* Copyright (c) Facebook, Inc. and its affiliates.
38+
*
39+
* This source code is licensed under the MIT license found in the
40+
* LICENSE file in the root directory of this source tree.
41+
*/

part4/notes-backend/build/static/js/main.88d3369d.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

part4/notes-backend/build/static/js/main.f4976be5.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
object-assign
3+
(c) Sindre Sorhus
4+
@license MIT
5+
*/
6+
7+
/** @license React v0.20.2
8+
* scheduler.production.min.js
9+
*
10+
* Copyright (c) Facebook, Inc. and its affiliates.
11+
*
12+
* This source code is licensed under the MIT license found in the
13+
* LICENSE file in the root directory of this source tree.
14+
*/
15+
16+
/** @license React v17.0.2
17+
* react-dom.production.min.js
18+
*
19+
* Copyright (c) Facebook, Inc. and its affiliates.
20+
*
21+
* This source code is licensed under the MIT license found in the
22+
* LICENSE file in the root directory of this source tree.
23+
*/
24+
25+
/** @license React v17.0.2
26+
* react-jsx-runtime.production.min.js
27+
*
28+
* Copyright (c) Facebook, Inc. and its affiliates.
29+
*
30+
* This source code is licensed under the MIT license found in the
31+
* LICENSE file in the root directory of this source tree.
32+
*/
33+
34+
/** @license React v17.0.2
35+
* react.production.min.js
36+
*
37+
* Copyright (c) Facebook, Inc. and its affiliates.
38+
*
39+
* This source code is licensed under the MIT license found in the
40+
* LICENSE file in the root directory of this source tree.
41+
*/

part4/notes-backend/build/static/js/main.f4976be5.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const notesRouter = require('express').Router()
2+
const Note = require('../models/note')
3+
4+
notesRouter.get('/', (request, response) => {
5+
Note.find({}).then(notes => {
6+
response.json(notes)
7+
})
8+
})
9+
10+
notesRouter.get('/:id', (request, response, next) => {
11+
Note.findById(request.params.id)
12+
.then(note => {
13+
if (note) {
14+
response.json(note)
15+
} else {
16+
response.status(404).end()
17+
}
18+
})
19+
.catch(error => next(error))
20+
})
21+
22+
notesRouter.post('/', (request, response, next) => {
23+
const body = request.body
24+
25+
const note = new Note({
26+
content: body.content,
27+
important: body.important || false,
28+
date: new Date(),
29+
})
30+
31+
note.save()
32+
.then(savedNote => {
33+
response.json(savedNote)
34+
})
35+
.catch(error => next(error))
36+
})
37+
38+
notesRouter.delete('/:id', (request, response, next) => {
39+
Note.findByIdAndRemove(request.params.id)
40+
.then(() => {
41+
response.status(204).end()
42+
})
43+
.catch(error => next(error))
44+
})
45+
46+
notesRouter.put('/:id', (request, response, next) => {
47+
const body = request.body
48+
49+
const note = {
50+
content: body.content,
51+
important: body.important,
52+
}
53+
54+
Note.findByIdAndUpdate(request.params.id, note, { new: true })
55+
.then(updatedNote => {
56+
response.json(updatedNote)
57+
})
58+
.catch(error => next(error))
59+
})
60+
61+
module.exports = notesRouter

part4/notes-backend/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const app = require('./app') // varsinainen Express-sovellus
2+
const http = require('http')
3+
const config = require('./utils/config')
4+
const logger = require('./utils/logger')
5+
6+
const server = http.createServer(app)
7+
8+
server.listen(config.PORT, () => {
9+
logger.info(`Server running on port ${config.PORT}`)
10+
})

part4/notes-backend/models/note.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const mongoose = require('mongoose')
2+
3+
const noteSchema = new mongoose.Schema({
4+
content: {
5+
type: String,
6+
required: true,
7+
minlength: 5
8+
},
9+
date: Date,
10+
important: Boolean,
11+
})
12+
13+
noteSchema.set('toJSON', {
14+
transform: (document, returnedObject) => {
15+
returnedObject.id = returnedObject._id.toString()
16+
delete returnedObject._id
17+
delete returnedObject.__v
18+
}
19+
})
20+
21+
module.exports = mongoose.model('Note', noteSchema)

part4/notes-backend/mongo.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const mongoose = require('mongoose')
2+
3+
if (process.argv.length<3) {
4+
console.log('give password as argument')
5+
process.exit(1)
6+
}
7+
8+
const password = process.argv[2]
9+
10+
const url =
11+
`mongodb+srv://fullstack:${password}@cluster0.o1opl.mongodb.net/noteApp?retryWrites=true&w=majority`
12+
13+
mongoose.connect(url)
14+
15+
const noteSchema = new mongoose.Schema({
16+
content: String,
17+
date: Date,
18+
important: Boolean,
19+
})
20+
21+
const Note = mongoose.model('Note', noteSchema)
22+
23+
const note = new Note({
24+
content: 'CSS is hard',
25+
date: new Date(),
26+
important: false,
27+
})
28+
29+
// eslint-disable-next-line no-constant-condition
30+
if ( false ) {
31+
note.save().then(() => {
32+
console.log('note saved!')
33+
mongoose.connection.close()
34+
})
35+
}
36+
37+
Note.find({}).then(result => {
38+
result.forEach(note => {
39+
console.log(note)
40+
})
41+
mongoose.connection.close()
42+
})

0 commit comments

Comments
 (0)