forked from Neil-140301/email-editor-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (51 loc) · 1.65 KB
/
server.js
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
57
58
59
60
61
62
63
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const path = require('path');
const mongoose = require('mongoose');
const app = express();
const port = process.env.PORT || 5000;
app.use(bodyParser.json());
app.use(cors());
//mongoose
mongoose.connect("mongodb+srv://neil:neil140301@cluster0.g5a3e.mongodb.net/emailDb");
//data schema and model
const templateSchema = {
templateId: Number,
templateJson: String
}
const Template = mongoose.model("Template",templateSchema);
//Api routes
app.get('/templates', async (request,response) => {
const results = await Template.find();
const templates = results.map(item => item.templateId)
response.send(templates);
});
app.get('/template/:id', async (request,response) => {
const {id} = request.params;
const template = await Template.find({templateId: id});
response.send(template);
});
app.post('/template', (request, response) => {
const {templateId, templateJson} = request.body;
console.log('hi');
const newTemplate = new Template({templateId,templateJson});
newTemplate.save();
});
app.put('/template/:id', async (request,response) => {
const {id} = request.params;
const {templateJson} = request.body;
console.log(templateJson)
const result = await Template.updateOne({templateId: id},{$set: {templateJson}})
response.send("updated");
});
if(process.env.NODE_ENV === 'production') {
app.use(express.static('frontend/build'));
app.get('*', (request,response) => {
response.sendFile(path.resolve(__dirname,'frontend','build','index.html'));
});
}
// server start
app.listen(port, () => {
console.log("express is running");
});