-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathapp.js
More file actions
48 lines (39 loc) · 1.27 KB
/
app.js
File metadata and controls
48 lines (39 loc) · 1.27 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
// Define variables & dependencies
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var mongoose = require('mongoose');
var async = require('async');
var location = require('./location')();
var controllers = require('./controller');
var Location = mongoose.model('Location');
var fs = require('fs');
var path = require('path');
// Bootstrap mongoose and load dummy data
mongoose.connect('mongodb://localhost/geospatial_db', function(err) {
if (err) throw err;
// load data from file and transform it to Object
var data = JSON.parse(fs.readFileSync(path.join(__dirname, 'data.json'), 'utf8'));
// clean db and load new data
Location.remove(function() {
async.each(data, function(item, callback) {
// create a new location
Location.create(item, callback);
}, function(err) {
if (err) throw err;
});
});
});
// Configure Express
app.configure(function() {
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
// Define routes
app.get('/', controllers.index);
app.get('/api/locations', controllers.findLocation);
// Start the server
server.listen(3000, function() {
console.log("Express server up and running on port 3000 - simple geolocation");
});