Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- // configurable portsetting
- var config = {
- httpPort: 8080,
- mongoPort: 27017
- }
- var express = require('express');
- var bodyParser = require('body-parser');
- var mongoose = require('mongoose');
- var app = express();
- app.use(bodyParser.urlencoded({extended: true})); // enable processing of the received post content
- /* database schema for features */
- var featureSchema = mongoose.Schema({
- name: String,
- dateInserted: Date,
- data: {}
- });
- var Feature = mongoose.model('Feature', featureSchema);
- /* database schema for routes */
- var routeSchema = mongoose.Schema({
- dateInserted: Date,
- data: {}
- });
- var Route = mongoose.model('Route', routeSchema);
- /* init database connection */
- mongoose.connect('mongodb://localhost:' + config.mongoPort + '/ex06DB');
- var database = mongoose.connection;
- database.on('error', console.error.bind(console, 'ABORTING. database connection error:'));
- // once db connection is open, start http server (Need to start db first, then server)
- database.once('open', function (callback) {
- console.log('connection to database established on port ' + config.mongoPort);
- app.listen(config.httpPort, function(){
- console.log('http server now running on port ' + config.httpPort);
- });
- });
- /** http routing **/
- // code which is executed on every request
- app.use(function(req, res, next) {
- console.log(req.method + ' ' + req.url + ' was requested by ' + req.connection.remoteAddress);
- res.header('Access-Control-Allow-Origin', '*'); // allow CORS
- next();
- });
- /* web app */
- // deliver all contents of the folder '/webapp' under '/'
- app.use(express.static(__dirname + '/webapp'));
- // takes a json document via POST, which will be added to the database
- // name is passed via URL
- // url format: /addFeature?name=
- app.post('/addFeature*', function(req, res) {
- var title = req.url.substring(17, req.url.length);
- var feature = new Feature({
- name: title,
- data: req.body
- });
- feature.save(function(error){
- var message = error ? 'failed to save feature: ' + error
- : 'feature saved: ' + feature.name;
- console.log(message + ' from ' + req.connection.remoteAddress);
- res.send(message);
- });
- });
- /**
- * saves the last drawn feature into the database
- */
- function saveToDB() {
- console.log("start: save to DB");
- var name = prompt('Please give a name for the feature:');
- var contentString = $('#drawnItemJSONText').val();
- if ( name != undefined && contentString != '' ) {
- var content = JSON.parse( contentString );
- var url = 'localhost:8080' + '/addFeature?name=' + name;
- // perform post ajax
- $.ajax({
- type: 'POST',
- data: content,
- url: url,
- timeout: 1000,
- success: function(data, textStatus ){
- console.log("feature succesfully loaded into the database");
- },
- error: function(xhr, textStatus, errorThrown){
- console.log("Sorry, could not load the feature into the database");
- }
- });
- } else {
- console.log("A Problem occured while adding to the database. No JSON-Object or name provided.");
- }
- };
- var url = 'http://localhost:8080' + '/addFeature?name=' + name;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement