12-23 19:35
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. 21. 23:01

[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 posts = require('./routes/posts');
var rem = require('./routes/remove');
var find = require('./routes/find');

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('/posts', posts);
app.use('/remove', rem);
app.use('/find', find);

// mongoose
var uri = 'mongodb://127.0.0.1/test';
mongoose.connect(uri, function(req, res){
    console.log('mongodb 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 variables for MongoDB model
global;

global.UserSchema = mongoose.Schema({
    name: String,
    age: Number
});

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);
});



[find.js]

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

router.get('/', function(req, res, next){
	global.User.findOne({name: req.query.name}, function(err, myUser){
		if(myUser != null) {
			res.send('Name : ' + myUser.name + ' Age : ' + myUser.age);
		}
		else {
			message = req.query.name + ' is not found.';
			res.send(message);
		};
	});
});

module.exports = router;



[DB]

> db.users.insert({name: "gchoi", age: 38})
WriteResult({ "nInserted" : 1 })
> db.users.insert({name: "jmpark", age: 26})
WriteResult({ "nInserted" : 1 })
> db.users.insert({name: "hskim", age: 26})
WriteResult({ "nInserted" : 1 })
> db.users.insert({name: "tjkwak", age: 32})
WriteResult({ "nInserted" : 1 })
> db.users.insert({name: "jschoi", age: 37})
WriteResult({ "nInserted" : 1 })
> db.users.find().pretty()
{
	"_id" : ObjectId("55366a0607518ae762c8518d"),
	"name" : "gchoi",
	"age" : 38
}
{
	"_id" : ObjectId("55366a1307518ae762c8518e"),
	"name" : "jmpark",
	"age" : 26
}
{
	"_id" : ObjectId("55366a1807518ae762c8518f"),
	"name" : "hskim",
	"age" : 26
}
{
	"_id" : ObjectId("55366a2707518ae762c85190"),
	"name" : "tjkwak",
	"age" : 32
}
{
	"_id" : ObjectId("55366a3307518ae762c85191"),
	"name" : "jschoi",
	"age" : 37
}



[Results]


Comments