12-23 09:34
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[Web App/Node.js Express] 웹 브라우저 주소창의 쿼리를 이용한 도큐먼트 변경 본문

Programming/Web App

[Web App/Node.js Express] 웹 브라우저 주소창의 쿼리를 이용한 도큐먼트 변경

cinema4dr12 2015. 4. 23. 17:51

[app.js]

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');

var routes = require('./routes/index');
var users = require('./routes/users');
var update = require('./routes/update');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hjs');

// port setup
app.set('port', process.env.PORT || 3000);

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);
app.use('/update', update);

// mongoose
var uri = 'mongodb://127.0.0.1/users';
mongoose.connect(uri, function(req, res){
    console.log('mongodb user connected');
});

uri = 'mongodb://127.0.0.1/om_config';
mongoose.connect(uri, function(req, res){
    console.log('mongodb OM configuration connected');
});

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

global;

// MongoDB UserSchema
global.UserSchema = mongoose.Schema({
    userName: String,
    userId: String,
    password: String
});

global.User = mongoose.model('User', UserSchema);

module.exports = app;

var server = app.listen(app.get('port'), function() {
  console.log('Express server listening on port ' + server.address().port);
});



[update.js]

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');


/* GET users listing. */
router.get('/', function(req, res, next){
	global.User.findOneAndUpdate({userName: req.query.userName}, 
		{password: req.query.password}, 
		function(err, doc){
			if(doc != null)
				res.send("success!");
			else
				res.send("fail");
		});
});

module.exports = router;



[results]

> Prepared documents

> db.users.find().pretty()
{
	"_id" : ObjectId("5537c80e56c5410f22e5e99e"),
	"userName" : "gchoi",
	"userId" : "cinema4d",
	"password" : "12345"
}
{
	"_id" : ObjectId("5537c82656c5410f22e5e99f"),
	"userName" : "jmpark",
	"userId" : "raspberry",
	"password" : "12345"
}
{
	"_id" : ObjectId("5537c83756c5410f22e5e9a0"),
	"userName" : "tjkwak",
	"userId" : "ozerodie",
	"password" : "12345"
}

> Query in Web Browser

> Modified documents

> db.users.find().pretty()
{
	"_id" : ObjectId("5537c80e56c5410f22e5e99e"),
	"userName" : "gchoi",
	"userId" : "cinema4d",
	"password" : "12345"
}
{
	"_id" : ObjectId("5537c82656c5410f22e5e99f"),
	"userName" : "jmpark",
	"userId" : "raspberry",
	"password" : "12345"
}
{
	"_id" : ObjectId("5537c83756c5410f22e5e9a0"),
	"userName" : "tjkwak",
	"userId" : "ozerodie",
	"password" : "54321"
}


Another Try: Input userName that does not exist by intention.


Comments