Permalink
Browse files

Studied Commit.

  • Loading branch information...
1 parent 32344c0 commit 263563a43d05cdbcafac0ec101fb1d45ae055cba @hk220 hk220 committed Mar 24, 2012
Showing with 219 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +102 −0 app.js
  3. +12 −0 package.json
  4. +8 −0 public/stylesheets/style.css
  5. +1 −0 public/test.csv
  6. +53 −0 route_setup.js
  7. +15 −0 routes/about.js
  8. +8 −0 routes/index.js
  9. +5 −0 views/about.jade
  10. +5 −0 views/index.jade
  11. +7 −0 views/layout.jade
View
@@ -0,0 +1,3 @@
+node_modules/
+.DS_Store
+*.swp
View
102 app.js
@@ -0,0 +1,102 @@
+
+/**
+ * Module dependencies.
+ */
+
+var express = require('express')
+ , routes = require('./routes')
+ , http = require('http');
+
+var app = module.exports = express();
+
+app.configure(function(){
+ app.set('views', __dirname + '/views');
+ app.set('view engine', 'jade');
+ app.use(express.favicon());
+ app.use(express.logger('dev'));
+ app.use(express.static(__dirname + '/public'));
+ app.use(express.bodyParser({
+ uploadDir: './uploaded_files'
+ }));
+ app.use(express.cookieParser('secret', 'hogehoge'));
+ app.use(express.session({key: 'session_id'}));
+ app.use(express.methodOverride());
+ app.use(function(req, res, next){
+ console.log('my own midleware');
+ next();
+ });
+ app.use(app.router);
+});
+
+app.configure('development', function(){
+ app.use(express.errorHandler());
+});
+
+app.get('/', routes.index);
+
+var about_handler = require('./routes/about.js');
+//app.get('/about/:id?.:format?', about_handler.index);
+//console.log(app.lookup.all('/about/:id?.:format?'));
+
+var fs = require('fs')
+ , path = require('path')
+ , routes_dir = path.resolve(__dirname + '/routes')
+var map_routes = function(dir) {
+ var files = fs.readdirSync(dir)
+ files.forEach(function(e){
+ e = dir + '/' + e
+ var stats = fs.statSync(e)
+ if(stats.isDirectory()){
+ map_routes(e)
+ }
+ else{
+ var url_path = '/'
+ if(path.basename(e, '.js') == 'index'){
+ url_path += path.dirname(path.relative(routes_dir, e))
+ }
+ else{
+ url_path += path.relative(routes_dir, e).replace('.js', '')
+ }
+ url_path = path.normalize(url_path)
+ var handler = require(e)
+ for(var f in handler){
+ switch(f){
+ case 'index':
+ app.get(url_path + '?', handler[f])
+ break;
+ case 'new':
+ app.get(url_path + '/new', handler[f])
+ break;
+ case 'create':
+ app.post(url_path, handler[f])
+ break;
+ case 'edit':
+ app.get(url_path + '/:id/edit', handler[f])
+ break;
+ case 'update':
+ app.put(url_path + '/:id', handler[f])
+ break;
+ case 'destroy':
+ app.del(url_path + '/:id', handler[f])
+ break;
+ case 'show':
+ app.get(url_path + '/:id.:format?', handler[f])
+ break;
+ default:
+ app.get(url_path + '/' + f, handler[f])
+ }
+ }
+ }
+ })
+}
+map_routes(routes_dir)
+
+app.locals.use(function(req, res){
+ app.locals.message = req.session.message || ''
+});
+
+//app.get('/download', about_handler.download);
+
+http.createServer(app).listen(3000);
+
+console.log("Express server listening on port 3000");
View
@@ -0,0 +1,12 @@
+{
+ "name": "application-name",
+ "version": "0.0.1",
+ "private": true,
+ "scripts": {
+ "start": "node app"
+ },
+ "dependencies": {
+ "express": "3.0.0alpha1-pre",
+ "jade": "*"
+ }
+}
@@ -0,0 +1,8 @@
+body {
+ padding: 50px;
+ font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
+}
+
+a {
+ color: #00B7FF;
+}
View
@@ -0,0 +1 @@
+123456
View
@@ -0,0 +1,53 @@
+var fs = require('fs')
+ , path = require('path')
+ , routes_dir = path.resolve(__dirname + '/routes')
+var map_routes = function(dir) {
+ var files = fs.readdirSync(dir)
+ files.forEach(function(e){
+ e = dir + '/' + e
+ var stats = fs.statSync(e)
+ if(stats.isDirectory()){
+ map_routes(e)
+ }
+ else{
+ var url_path = '/'
+ if(path.basename(e, '.js') == 'index'){
+ url_path += path.dirname(path.relative(routes_dir, e))
+ }
+ else{
+ url_path += path.relative(routes_dir, e).replace('.js', '')
+ }
+ url_path = path.normalize(url_path)
+ var handler = require(e)
+ for(var f in handler){
+ switch(f){
+ case 'index':
+ app.get(url_path + '?', handler[f])
+ break;
+ case 'new':
+ app.get(url_path + '/new', handler[f])
+ break;
+ case 'create':
+ app.post(url_path, handler[f])
+ break;
+ case 'edit':
+ app.get(url_path + '/:id/edit', handler[f])
+ break;
+ case 'update':
+ app.put(url_path + '/:id', handler[f])
+ break;
+ case 'destroy':
+ app.del(url_path + '/:id', handler[f])
+ break;
+ case 'show':
+ app.get(url_path + '/:id.:format?', handler[f])
+ break;
+ default:
+ app.get(url_path + '/' + f, handler[f])
+ }
+ }
+ }
+ })
+}
+map_routes(routes_dir)
+
View
@@ -0,0 +1,15 @@
+var app = module.parent.exports
+exports.index = function(req, res){
+ console.log(req.params.id);
+ console.log(req.params.format);
+ res.render('about',{number: req.params.id, title:'about'});
+}
+
+exports.download = function(req, res){
+ var path = require('path')
+ var file = path.normalize(__dirname + '/../public/test.csv')
+ path.exists(file, function(exists){
+ console.log(exists)
+ })
+ res.sendfile(file)
+}
View
@@ -0,0 +1,8 @@
+
+/*
+ * GET home page.
+ */
+
+exports.index = function(req, res){
+ res.render('index', { title: 'Express' });
+};
View
@@ -0,0 +1,5 @@
+extends layout
+block content
+ h1 jadeテンプレートを表示します。
+ h2 #{message}
+ h2 #{number}
View
@@ -0,0 +1,5 @@
+extends layout
+
+block content
+ h1= title
+ p Welcome to #{title}
View
@@ -0,0 +1,7 @@
+!!!
+html
+ head
+ title= title
+ link(rel='stylesheet', href='/stylesheets/style.css')
+ body
+ block content

0 comments on commit 263563a

Please sign in to comment.