Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. "use strict";
  2.  
  3. // configurable portsetting
  4. var config = {
  5. httpPort: 8080,
  6. mongoPort: 27017
  7. }
  8.  
  9. var express = require('express');
  10. var bodyParser = require('body-parser');
  11. var mongoose = require('mongoose');
  12.  
  13. var app = express();
  14. app.use(bodyParser.urlencoded({extended: true})); // enable processing of the received post content
  15.  
  16. /* database schema for features */
  17. var featureSchema = mongoose.Schema({
  18. name: String,
  19. dateInserted: Date,
  20. data: {}
  21. });
  22. var Feature = mongoose.model('Feature', featureSchema);
  23.  
  24. /* database schema for routes */
  25. var routeSchema = mongoose.Schema({
  26. dateInserted: Date,
  27. data: {}
  28. });
  29. var Route = mongoose.model('Route', routeSchema);
  30.  
  31. /* init database connection */
  32. mongoose.connect('mongodb://localhost:' + config.mongoPort + '/ex06DB');
  33. var database = mongoose.connection;
  34.  
  35. database.on('error', console.error.bind(console, 'ABORTING. database connection error:'));
  36.  
  37. // once db connection is open, start http server (Need to start db first, then server)
  38. database.once('open', function (callback) {
  39.  
  40. console.log('connection to database established on port ' + config.mongoPort);
  41. app.listen(config.httpPort, function(){
  42. console.log('http server now running on port ' + config.httpPort);
  43. });
  44. });
  45.  
  46.  
  47. /** http routing **/
  48.  
  49. // code which is executed on every request
  50. app.use(function(req, res, next) {
  51. console.log(req.method + ' ' + req.url + ' was requested by ' + req.connection.remoteAddress);
  52. res.header('Access-Control-Allow-Origin', '*'); // allow CORS
  53. next();
  54. });
  55.  
  56. /* web app */
  57.  
  58. // deliver all contents of the folder '/webapp' under '/'
  59. app.use(express.static(__dirname + '/webapp'));
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. // takes a json document via POST, which will be added to the database
  67. // name is passed via URL
  68. // url format: /addFeature?name=
  69. app.post('/addFeature*', function(req, res) {
  70. var title = req.url.substring(17, req.url.length);
  71. var feature = new Feature({
  72. name: title,
  73. data: req.body
  74. });
  75. feature.save(function(error){
  76. var message = error ? 'failed to save feature: ' + error
  77. : 'feature saved: ' + feature.name;
  78. console.log(message + ' from ' + req.connection.remoteAddress);
  79. res.send(message);
  80. });
  81. });
  82.  
  83. /**
  84. * saves the last drawn feature into the database
  85. */
  86. function saveToDB() {
  87. console.log("start: save to DB");
  88.  
  89. var name = prompt('Please give a name for the feature:');
  90. var contentString = $('#drawnItemJSONText').val();
  91.  
  92.  
  93.  
  94. if ( name != undefined && contentString != '' ) {
  95.  
  96. var content = JSON.parse( contentString );
  97. var url = 'localhost:8080' + '/addFeature?name=' + name;
  98.  
  99. // perform post ajax
  100. $.ajax({
  101. type: 'POST',
  102. data: content,
  103. url: url,
  104. timeout: 1000,
  105. success: function(data, textStatus ){
  106. console.log("feature succesfully loaded into the database");
  107. },
  108. error: function(xhr, textStatus, errorThrown){
  109. console.log("Sorry, could not load the feature into the database");
  110. }
  111. });
  112. } else {
  113. console.log("A Problem occured while adding to the database. No JSON-Object or name provided.");
  114. }
  115. };
  116.  
  117. var url = 'http://localhost:8080' + '/addFeature?name=' + name;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement