forked from danielstern/express-react-fullstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
56 lines (41 loc) · 1.29 KB
/
server.js
File metadata and controls
56 lines (41 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { MongoClient } from 'mongodb';
import path from 'path';
import express from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import './initialize-db';
import { authenticationRoute } from './authenticate'
import { connectDB } from './connect-db'
import { addNewTask, updateTask } from './communicate-db';
let port = process.env.PORT || 7777;
let app = express();
app.use(
cors(),
bodyParser.urlencoded({extended:true}),
bodyParser.json()
);
app.listen(port,console.info("Server running, listening on port ", port));
authenticationRoute(app);
if (process.env.NODE_ENV == `production`) {
app.use(express.static(path.resolve(__dirname,'../../dist')));
app.get('/*',(req,res)=>{
res.sendFile(path.resolve('index.html'));
});
}
app.post('/task/new',async (req,res)=>{
// let task = req.body.task;
await addNewTask(req.body.task);
res.status(200).send();
});
app.post('/task/update',async (req,res)=>{
let db = await connectDB();
await updateTask(req.body.task);
res.status(200).send();
});
app.post('/comment/new',async (req,res)=>{
let comment = req.body.comment;
let db = await connectDB();
let collection = db.collection(`comments`);
await collection.insertOne(comment);
res.status(200).send();
});