javascript - express, mongoose passing param from 1 app.get to another -
my problem cannot quite figure out how "name" parameter before run second app.get.. first (app.get /) lets me choose between collections, , pass "name" param second app can req.params.name within second app.get...
problem need param before second app.get cuz need define schema , "compile" model, can done once. works when click once, "cannot owerwrite model, compiled" error when again. bypassed problem opening new connection everytime load second app.get (not know).
so if knows how "name" param (the thing im passing req.param second app.get, before actual second app.get fires up, thankful)
my code: (started learning node 2 days ago, alongside express , mongoose, not mention javascript sp go easy on me :p)
var express = require('express') ,routes = require('./routes') ,user = require('./routes/user') ,http = require('http') ,path = require('path') ,mongoose = require("mongoose"); var app = express(); // environments app.set('port', process.env.port || 3005); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.favicon()); app.use(express.logger('dev')); app.use(express.bodyparser()); app.use(express.methodoverride()); app.use(app.router); app.use(express.static(path.join(__dirname, 'public'))); // development if ('development' == app.get('env')) { app.use(express.errorhandler()); } conn = mongoose.createconnection("mongodb://10.42.1.31/dominik"); app.get ("/", function(req, res) { conn.db.collectionnames(function (err, docs) { res.render("collections", {names:docs}); }); }); app.get("/collections/:name", function (req,res) { conn = mongoose.createconnection("mongodb://10.42.1.31/dominik"); var collname = req.params.name; collname = collname.charat(0).touppercase()+collname.slice(1); collname = collname.substring(0, collname.length-1); var schema = new mongoose.schema({}); var mymodel= conn.model(collname, schema); mymodel.find({},function(err, docs) { res.json("documents",docs); }); }); http.createserver(app).listen(app.get('port'), function(){ console.log('express server listening on port ' + app.get('port')); });
Comments
Post a Comment