실행방법
mongod - 몽고 서버띄움
node app.js - 노드 서버띄움
url
app.js
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var http = require('http');
var db = require('./mongo.js');
var users = require('./routes/users');
var app = express();
db.connect();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
/*
app.use('/', function(req, res) {
db.findOneUser({username:"admin"}, function(item) {
console.log(item);
});
res.render('index', { title: 'Express' });
});
*/
app.get('/login', function(req, res) {
res.render('login', {title:'LOGIN'});
});
/*
app.post('/login', function(req, res) {
res.render('index', {title: 'INDEX'});
});
*/
app.get('/add', function(req, res) {
res.render('add', {title:'ADD POST'});
});
/* app.post('/addpost', function(req, res){}); */
/// 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: {}
});
});
module.exports = app;
http.createServer(app).listen(3000, function() {
console.log("Server started at 3000 port");
});
mongo.js
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
/*
exports.deleteData = function(table, query, callback) {
tb = db.collection(table);
tb.update(query, {$set:{deleted:1}});
};
//!QUERY DATA
exports.findData = function(table, data, callback) {
tb = db.collection(table);
tb.find(data).toArray(function(err, items) {
// if (err) console.log("find err : " + err);
callback(err, items);
});
};
exports.findOneData = function(table, data, callback) {
tb = db.collection(table);
tb.findOne(data, function(err, item) {
// if (err) console.log("findOne err : " + err);
callback(err, item);
});
};
*/
var db, User;
exports.connect = function() {
MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, data) {
if (err) throw err;
db = data;
User = db.collection('User');
console.log('db connected');
/*
// 쿼리
User.find({}).toArray(function(err, items) {
console.log(items);
});
User.update({username:"admin2"}, {$set: {password:"admin3"}}, function(err, success){
console.log(success);
});
// 인서트
User.insert({username:"admin2", password:"admin2"}, function(err, items) {
console.log(items);
});
*/
});
}
exports.findOneUser = function(query, callback) {
User.findOne(query, function(err, item) {
callback(item);
});
};
layout.jade
doctype html
html
head
meta(name='viewport', content='width=device-width, user-scalable=no, initial-scale=1.0', charset='utf-8')
meta(name="apple=mobile-web-app-capable", content="yes")
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
//script(src='/jquery-mobile/jquery-1.9.1.min.js')
//script(src='http://code.jquery.com/jquery-1.9.1.js')
//script(src='/javascripts/layout.js')
//script(src='/jquery-mobile/jquery.mobile-1.3.1.min.js')
//link(rel='stylesheet', href='/jquery-mobile/jquery.mobile.flatui.css')
//link(rel='stylesheet', href='/jquery-mobile/jquery.mobile-1.3.1.css')
//link(rel='stylesheet', href='http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css')
//script(src='http://code.jquery.com/jquery-latest.min.js')
//link(rel='stylesheet', href='http://ququplay.github.io/jquery-mobile-flat-ui-theme/css/jquery.mobile.flatui.css')
//link(rel='stylesheet', href='http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css')
//script(src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js")
body
block content
login.jade
extends layout
block content
h1= title
p Welcome to #{title}
form(method='post', action='')
.row
//<label class="col-md-3" id="user">
label.col-md-3#user user
input.col-md-9(name='user')
br
label.col-md-3 password
input.col-md-9(type='password' name='password')
.row
.col-xs-6.text-center
input.btn.btn-success(type='submit', value='login', style='width:90%; margin:10px auto')
.col-xs-6.text-center
input.btn.btn-danger(type='submit', value='join', style='width:90%; margin:10px 10px')
br
add.jade
extends layout
block content
h1 Add new blog post
form(name="add-post", method="post", action="/addpost")
.row
div
label Subject:
input(type="text", name="subject")
.row
div
label Content
textarea(name="body", rows="10", cols="30")
.row
input.btn.btn-success(type="submit", name="post", value="Post")
반응형
'Study' 카테고리의 다른 글
cocos2dx (0) | 2014.08.31 |
---|---|
한틀아리 4주 (0) | 2014.08.07 |
express.js post pending problem (0) | 2014.07.08 |
NodeJS Express 3.0 에서 Express 4.0 으로 (0) | 2014.05.29 |
한틀 스터디 (4/13) (0) | 2014.04.16 |