-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathapp.js
More file actions
117 lines (98 loc) · 2.99 KB
/
app.js
File metadata and controls
117 lines (98 loc) · 2.99 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
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
var express = require('express'),
multer = require('multer'),
path = require('path'),
fs = require('fs'),
bodyParser = require('body-parser')
var app = express()
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'app')));
app.use('/bower_components', express.static(path.join(__dirname, '/bower_components')));
/////db
var mongoose = require('mongoose');
// Connect to mongodb
var connect = function () {
var options = {server: {socketOptions: {keepAlive: 1}}};
mongoose.connect('mongodb://localhost:27017/grapheditor', options);
};
connect();
var Schema = mongoose.Schema;
var GraphSchema = new Schema({name: String, json: String, date: {type: Date, default: Date.now}});
//定义静态方法
GraphSchema.statics.findByName = function (name, cb) {
this.find({name: new RegExp(name, 'i')}, cb);
}
var Graph = mongoose.model('graphs', GraphSchema);
function saveGraph(name, json, cb) {
var graph = new Graph({name: name, json: json});
graph.save(cb);
return graph;
}
mongoose.connection.on('error', console.log);
mongoose.connection.on('disconnected', connect);
// Start the application after the database connection is ready
app.listen(3000);
console.log("Listening on port 3000");
function dropDB(cb) {
Graph.remove({}, function (err) {
if (err) return console.error(err);
if (cb) {
cb.call();
}
})
}
//dropDB();
var defaultGraph, defaultGraphName = 'Default Graph';
function initDB() {
dropDB(function () {
var json = fs.readFileSync('data/default.json', 'utf8');
defaultGraph = saveGraph(defaultGraphName, json);
})
}
Graph.findOne({name: defaultGraphName}, function (err, graph) {
if (!graph) {
return initDB();
}
defaultGraph = graph;
});
app.get('/data', function (req, res) {
if (!defaultGraph) {
return;
}
res.send(defaultGraph);
})
app.post('/save', function (req, res) {
var json = req.body.json;
if (json) {
var name = req.body.name;
if (name && name != defaultGraphName) {
return saveGraph(req.body.name, json, function (err, saved) {
if (err) return console.error(err);
res.send('save success - ' + name);
})
}
defaultGraph.json = json;
defaultGraph.save(function (err, updated) {
if (err) return console.error(err);
res.send('save success - ' + defaultGraphName);
});
}
})
app.get("/list", function (req, res) {
Graph.find(function (err, list) {
if (err) return console.error(err);
res.send(list);
})
});
app.get('/data/:id', function (req, res) {
Graph.findById(req.params.id, function (err, model) {
if (err) return console.error(err);
res.send(model);
})
})
app.post('restore', function(req, res){
initDB();
})