Skip to content

Commit 3f96432

Browse files
committed
Updating for csp
Signed-off-by: thomas turner <thomastdt@gmail.com>
1 parent 5f235a0 commit 3f96432

File tree

10 files changed

+998
-125
lines changed

10 files changed

+998
-125
lines changed

backend/app.js

Lines changed: 99 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
require('dotenv').config();
2-
const express = require('express');
3-
const path = require('path');
4-
const cluster = require('cluster');
5-
const bodyParser = require('body-parser');
6-
const Note = require('./models/note');
7-
const numCPUs = require('os').cpus().length;
8-
const isDev = process.env.NODE_ENV !== 'production';
9-
const PORT = 5000;
1+
require("dotenv").config();
2+
const express = require("express");
3+
// const helmet = require("helmet");
4+
const path = require("path");
5+
const cluster = require("cluster");
6+
// const bodyParser = require("body-parser");
7+
const Note = require("./models/note");
8+
9+
const numCPUs = require("os").cpus().length;
10+
const isDev = process.env.NODE_ENV !== "production";
11+
const PORT = 5000;
1012

1113
// Multi-process to utilize all CPU cores.
1214
if (!isDev && cluster.isMaster) {
@@ -17,91 +19,118 @@ if (!isDev && cluster.isMaster) {
1719
cluster.fork();
1820
}
1921

20-
cluster.on('exit', (worker, code, signal) => {
21-
console.error(`Node cluster worker ${worker.process.pid} exited: code ${code}, signal ${signal}`);
22+
cluster.on("exit", (worker, code, signal) => {
23+
console.error(
24+
`Node cluster worker ${worker.process.pid} exited: code ${code}, signal ${signal}`
25+
);
2226
});
23-
2427
} else {
2528
const app = express();
26-
//Middleware for JSON
27-
app.use(bodyParser.json());
29+
//Middleware for JSON
30+
// app.use(bodyParser.json());
31+
app.use(express.json());
2832
// Priority serve any static files.
29-
app.use(express.static(path.resolve(__dirname, '../build')));
33+
app.use(express.static(path.resolve(__dirname, "../build")));
3034

3135
// Answer API requests.
3236

33-
app.put('/api/notes/:id', (request, response) => {
34-
const { title, content } = request.body;
35-
const note = {
36-
title: title,
37-
content: content
38-
};
39-
console.log(request.body);
37+
app.put("/api/notes/:id", (request, response) => {
38+
const { title, content } = request.body;
39+
const note = {
40+
title: title,
41+
content: content,
42+
};
43+
console.log(request.body);
4044

41-
Note.findByIdAndUpdate(request.params.id, note, {new: true})
42-
.then(updatedNote => {
43-
response.json(updatedNote);
44-
})
45-
.catch(err => {
46-
console.error(err);
47-
response.status(400).send({ error: 'malformatted id' });
48-
});
45+
Note.findByIdAndUpdate(request.params.id, note, { new: true })
46+
.then((updatedNote) => {
47+
response.json(updatedNote);
48+
})
49+
.catch((err) => {
50+
console.error(err);
51+
response.status(400).send({ error: "malformatted id" });
52+
});
4953
});
50-
app.delete('/api/notes/:id', (request, response) => {
51-
Note.findByIdAndRemove(request.params.id)
52-
.then(result => {
53-
response.status(204).end();
54-
})
55-
.catch(err => { //simple error handling for now
56-
console.error(err);
57-
response.status(404).end();
58-
});
54+
app.delete("/api/notes/:id", (request, response) => {
55+
Note.findByIdAndRemove(request.params.id)
56+
.then((result) => {
57+
response.status(204).end();
58+
})
59+
.catch((err) => {
60+
//simple error handling for now
61+
console.error(err);
62+
response.status(404).end();
63+
});
5964
});
6065

61-
app.post('/api/notes', (request, response) => {
62-
const note = request.body;
66+
app.post("/api/notes", (request, response) => {
67+
const note = request.body;
6368

64-
new Note({
65-
title : note.title,
66-
content: note.content,
67-
date : new Date()
68-
})
69-
.save()
70-
.then(savedNote => {
71-
response.json(savedNote);
72-
})
69+
new Note({
70+
title: note.title,
71+
content: note.content,
72+
date: new Date(),
73+
})
74+
.save()
75+
.then((savedNote) => {
76+
response.json(savedNote);
77+
});
7378
});
7479

75-
app.get('/api/notes/:id', (request, response) => {
76-
Note.findById(request.params.id)
77-
.then(note => {
78-
if(note){
79-
response.json(note);
80-
} else {
81-
response.status(404).end();
82-
}
83-
})
84-
.catch(err => {
85-
console.error(err);
86-
response.status(400).send({error: 'malformatted id'});
87-
});
80+
app.get("/api/notes/:id", (request, response) => {
81+
Note.findById(request.params.id)
82+
.then((note) => {
83+
if (note) {
84+
response.json(note);
85+
} else {
86+
response.status(404).end();
87+
}
88+
})
89+
.catch((err) => {
90+
console.error(err);
91+
response.status(400).send({ error: "malformatted id" });
92+
});
8893
});
8994

90-
app.get('/api/notes', function (request, response) {
91-
/* res.set('Content-Type', 'application/json');
95+
app.get("/api/notes", function (request, response) {
96+
/* res.set('Content-Type', 'application/json');
9297
res.send('{"message":"Hello from the custom server!"}');
9398
*/
94-
Note.find({}).then(notes => {
95-
response.json(notes);
99+
Note.find({}).then((notes) => {
100+
response.json(notes);
96101
});
97102
});
98103

104+
// app.use((req, res, next) => {
105+
// // nonce should be base64 encoded
106+
// res.locals.styleNonce = crypto.randomBytes(32).toString("hex");
107+
// next();
108+
// });
109+
110+
// app.use(
111+
// helmet.contentSecurityPolicy({
112+
// directives: {
113+
// defaultSrc: ["'self'"],
114+
// objectSrc: ["'none'"],
115+
// baseUri: ["'none'"],
116+
// styleSrc: ["'self'", (req, res) => `'nonce-${res.locals.styleNonce}'`],
117+
// scriptSrc: ["'strict-dynamic'", "'report-sample'", "https:", "http:"],
118+
// },
119+
// })
120+
// );
99121
// All remaining requests return the React app, so it can handle routing.
100-
app.get('*', function(request, response) {
101-
response.sendFile(path.resolve(__dirname, '../build', 'index.html'));
122+
app.get("*", function (request, response) {
123+
response.sendFile(path.resolve(__dirname, "../build", "index.html"));
124+
// response.render(path.resolve(__dirname, "../build", "index.html"), {
125+
// styleNonce: res.locals.styleNonce,
126+
// });
102127
});
103128

104129
app.listen(PORT, function () {
105-
console.log(`Node ${isDev ? 'dev server' : 'cluster worker '+process.pid}: listening on port ${PORT}`);
130+
console.log(
131+
`Node ${
132+
isDev ? "dev server" : "cluster worker " + process.pid
133+
}: listening on port ${PORT}`
134+
);
106135
});
107136
}

0 commit comments

Comments
 (0)