-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
136 lines (114 loc) · 3.58 KB
/
app.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//Run node app.js to run this server script
//use curl -v -X POST -H 'Content-Type: application/xml' -d '<hello>world</hello>' http://localhost:3000/parsexml
/*
fieldinpural : [{
type: Schema.Types.ObjectId,
ref: 'cname'
}]
*/
const express = require('express');
const path = require('path');
const fs = require('fs');
const zipFolder = require('bestzip');
const convert = require('xml-js');
const Dock = require('./Dock');
const ModelsGenerator = require('./modelsgenerator');
const app = express();
const bodyParser = require('body-parser');
require('body-parser-xml-json')(bodyParser);
app.use(bodyParser.json());
app.use(bodyParser.xml());
app.use(express.static('public'));
let models = [
{
cname: "User",
schemaname: function(){
return this.cname+'Schema';
}
},
{
cname: "Blog",
schemaname: function(){
return this.cname+'Schema';
}
}
];
app.get('/', (req, res) => {
res.status(200).send("Hello Server is Working")
});
app.post('/download', (req, res) => {
let DockObj = new Dock('userid', 'modelid');
let modelDir = DockObj.modelDir;
let userDir = DockObj.userDir;
let zip = `${userDir}/Diagram1.zip`;
DockObj.genModelFiles(models)
.then(() => {
return zipFolder({
source: modelDir,
destination: zip
});
})
.then(function() {
console.log('all done!');
})
.catch(err => {
console.log(err);
res.status(304).send("ERROR");
});
});
app.post('/gendownload', (req, res)=> {
let DockObj = new Dock('userid', 'modelid');
let modelDir = DockObj.modelDir;
let userDir = DockObj.userDir;
let zip = `${userDir}/models.zip`;
DockObj.genModelFiles(ModelsGenerator(req.body))
.then(() => {
zipFolder(modelDir, zip, (err) => {
if(err) {
console.log('oh no! Error compressing', err);
res.status(304).send("ERROR");
} else {
console.log('EXCELLENT');
DockObj.cleanUpModelDir();
res.download(zip, 'models.zip');
}
});
})
.catch(err => {
console.log(err);
res.status(304).send("ERROR");
});
});
app.get('/samplejsondata', (req, res) => {
res.status(200).json({"Hello": "World"});
});
app.post('/save', (req, res, body) => {
console.log(req.query.filename);
res.json(req.body);
});
app.get('/samplexmldata', (req, res) => {
fs.readFile('hello.xml', (err, data) => {
res.set('Content-Type','application/xml');
res.send(data);
});
});
//Parse the XML receieved on Post request and send it back
app.post('/parsexml', function(req, res, body) {
// Any request with an XML payload will be parsed
// and a JavaScript object produced on req.body
// corresponding to the request payload.
console.log(req.body)
res.status(200).json(req.body);
});
app.get('/examplejson', (req, res) => {
fs.readFile('hello1.xml', (err, data) => {
let jsobj = JSON.parse(convert.xml2json(data));
let modelPrimitives = ModelsGenerator(jsobj);
console.log(modelPrimitives);
res.json(modelPrimitives);
});
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log('listening on port :'+PORT);
});