>npm install express
>npm install routes
>npm install jade
1. app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | /* 폴더 구조 1. /node_modules - nodejs module이 설치될 공간 2. /public - connect 모듈의 Static 미들웨어를 사용하여 웹 서버에 올라가는 폴더 (js, css, images등 리소스 생성) 3. /routes - 페이지 라우트와 관련된 모듈 (index.js) 4. /views - EJS, Jade 파일과 같은 템플릿 파일 express 프레임워크 설정 1. basepath - 기본 URL 지정 2. views - 뷰 폴더 지정 3. view engine - 뷰 엔진 지정 4. view option - 뷰 엔진 옵션 지정 5. view cache - 뷰 캐시를 사용할지 지정 6. case sensitive routes - 페이지 라우트를 할 때 대소문자를 구분할지 지정 7. stric routing - 엄격 경로 확정을 사용할지 지정 */ //Module var express = require( 'express' ), routes = require( './routes' ); var app = module.exports = express(); //Configuration app.configure( function (){ app.set( 'views' , __dirname + '/views' ); app.set( 'view engine' , 'jade' ); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(__dirname + '/public' )); }); // 실행 환경 설정 app.configure( 'development' , function (){ app.use(express.errorHandler({dumpExceptions: true , showStack: true })); }); app.configure( 'production' , function (){ app.use(express.errorHandler()); }); //Routes // GET - / app.get( '/' , routes.index); app.get( '/redirect' , routes.redirect); app.get( '/product' , routes.product); app.get( '/product/insert' , routes.product.insert); app.get( '/product/edit' , routes.product.edit); app.listen(3000); |
2. /routes/index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | /* get hompage */ exports.index = function (req, res){ res.render( 'index' , {title: 'Express' }); } exports.life = function (req, res){ res.writeHead(200, { 'Content-Type' : 'text/html' }); res.end( '<h1>Life Page</h1>' ); } exports.redirect = function (req, res){ //페이지 강제 이동 //res.writeHead(302, {'Location' : 'http://applenamu3.blog.me'}); //res.end(); } // GET - /product exports.product = function (req, res){ res.render( "product" , { title : 'Product Page' }); } // GET - /product/insert exports.product.insert = function (req, res){ res.render( "product/insert" ,{ title: 'Insert Page' }); } // GET - /product/edit exports.product.edit = function (req, res){ res.render( 'product/edit' , { title: 'Edit Page' }); } |
3. /views/index.jade, /views/product/index.jade, insert.jade, edit.jade
1 2 3 | h1= title p Node.js Programing for Modern Web Development hr |
'프로그래밍 > nodeJS' 카테고리의 다른 글
[nodeJS] Socket.IO 방만들기 (0) | 2015.01.27 |
---|---|
[nodeJS] Socket.IO 모듈 (0) | 2015.01.27 |
[nodeJS] connect / connect_router 모듈 (0) | 2015.01.27 |
[nodeJS] jade 모듈 (0) | 2015.01.27 |
[nodeJS] ejs 모듈 (0) | 2015.01.27 |