summaryrefslogtreecommitdiffstats
path: root/stepped-solutions/27/backend/src/index.js
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2018-08-10 13:14:27 -0400
committerWes Bos <wesbos@gmail.com>2018-08-10 13:14:27 -0400
commitebb84f1f01396cf75e7fb70bb0e2987bbc0b14b2 (patch)
tree17030a43e9d6a617f8ab269195823096dce071ea /stepped-solutions/27/backend/src/index.js
parent6daab5e176382954d2229d8a9251ae819572a29e (diff)
30
Diffstat (limited to 'stepped-solutions/27/backend/src/index.js')
-rwxr-xr-xstepped-solutions/27/backend/src/index.js33
1 files changed, 33 insertions, 0 deletions
diff --git a/stepped-solutions/27/backend/src/index.js b/stepped-solutions/27/backend/src/index.js
new file mode 100755
index 0000000..d95ec58
--- /dev/null
+++ b/stepped-solutions/27/backend/src/index.js
@@ -0,0 +1,33 @@
+const cookieParser = require('cookie-parser');
+const jwt = require('jsonwebtoken');
+
+require('dotenv').config({ path: 'variables.env' });
+const createServer = require('./createServer');
+const db = require('./db');
+
+const server = createServer();
+
+server.express.use(cookieParser());
+
+// decode the JWT so we can get the user Id on each request
+server.express.use((req, res, next) => {
+ const { token } = req.cookies;
+ if (token) {
+ const { userId } = jwt.verify(token, process.env.APP_SECRET);
+ // put the userId onto the req for future requests to access
+ req.userId = userId;
+ }
+ next();
+});
+
+server.start(
+ {
+ cors: {
+ credentials: true,
+ origin: process.env.FRONTEND_URL,
+ },
+ },
+ deets => {
+ console.log(`Server is now running on port http://localhost:${deets.port}`);
+ }
+);