이제 지금까지 배운 것을 통해서 Express 프레임워크를 통해 간단한 웹 애플리케이션을 만들어보자.
기존의 app.js에 추가하면 가독성이 떨어지므로, 프로젝트에 새로 app_file.js 파일을 만들어 진행하자.
이번 애플리케이션 실습을 통해 파일 시스템 모듈도 활용할 것이다.
데이터 값을 저장할 data 폴더, html로 나타낼 pug 파일을 저장할 views_file 폴더를 만들자.
이제 app_file.js를 작성해보도록 하자
1 2 3 4 5 6 7 | var express = require('express'); var fs = require('fs'); // 파일 시스템 모듈 생성 var app = express(); // 애플리케이션 객체 생성 app.listen(3000, function(){ console.log('Connected, 3000 port!'); }) | cs |
기존의 express를 require하고 app 변수로 express 객체를 생성하는 것에 추가로 fs 모듈을 생성한다.
listen을 통해 포트 번호 3000으로 접속시킨 기본 틀을 잡았다.
set을 통해 views에 있는 pug 파일을 사용할 수 있도록 선언하자.
1 2 | app.set('views', './views_file'); app.set('view engine', 'pug'); | cs |
1 2 3 4 5 6 7 8 9 10 11 12 | doctype html html head meta(charset='utf-8') body form(action='/topic' method='post') p input(type='text' name='title' placeholder='title') p textarea(name='description') p input(type='submit') | cs |
1 2 3 | app.get('/topic/new', function(req, res){ res.render('new'); }); | cs |
/topic이라는 path에 post를 이용해서 제목과 내용을 전송하는 코드를 작성해보자.
1 2 3 4 5 6 7 8 9 10 11 | app.post('/topic', function(req, res){ var title = req.body.title; var description = req.body.description; fs.writeFile('data/'+title, description, function(err){ if(err){ console.log(err); res.status(500).send('Internal Server Error'); } res.send('Success!'); }) }) | cs |
1 2 3 | var bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: false })) | cs |
1 2 3 4 5 6 7 8 9 | app.get('/topic', function(req,res){ fs.readdir('data', function(err, files){ if(err){ console.log(err); res.status(500).send('Internal Server Error'); } res.render('view', {topics:files}); }) }); | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | doctype html html head meta(charset='utf-8') body h1 a(href='/topic') Server Side JavaScript ul each topic in topics li a(href='/topic/'+topic)= topic article h2= title h4= description | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | app.get('/topic/:id', function(req, res){ var id = req.params.id; fs.readdir('data', function(err, files){ if(err){ console.log(err); res.status(500).send('Internal Server Error'); } fs.readFile('data/'+id, 'utf8', function(err, data){ if(err){ console.log(err); res.status(500).send('Internal Server Error'); } res.render('view', {topics:files, title:id, description:data}); }) }) }) | cs |
1 2 3 4 5 6 7 8 9 | app.get('/topic/new', function(req, res){ fs.readdir('data', function(err, files){ if(err){ console.log(err); res.status(500).send('Internal Server Error'); } res.render('new', {topics:files}); }) }) | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | doctype html html head meta(charset='utf-8') body h1 a(href='/topic') Server Side JavaScript ul each topic in topics li a(href='/topic/'+topic)= topic article form(action='/topic' method='post') p input(type='text' name='title' placeholder='title') p textarea(name='description') p input(type='submit') | cs |
1 2 | div a(href='/topic/new') new | cs |
1 2 3 | app.get(['/topic', '/topic/:id'], function(req,res){ ... }) | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | app.get(['/topic', '/topic/:id'], function(req,res){ fs.readdir('data', function(err, files){ if(err){ console.log(err); res.status(500).send('Internal Server Error'); } var id = req.params.id; if(id){ //id값이 있을 때 fs.readFile('data/'+id, 'utf8', function(err, data){ if(err){ console.log(err); res.status(500).send('Internal Server Error'); } res.render('view', {topics:files, title:id, description:data}); }) } else{ //id값이 없을 때 res.render('view', {topics:files, title:'Welcome', description:'Hello, JavaScript for server.'}); } }) }) | cs |
'Node.js' 카테고리의 다른 글
[Node.js] 핵심 요약 (0) | 2018.09.26 |
---|---|
[Node.js] 6. 파일 업로드 (0) | 2018.04.04 |
[Node.js] 4. Express 프레임워크 (get과 post) (0) | 2018.04.02 |
[Node.js] 3. Express 프레임워크(템플릿, URL) (0) | 2018.04.02 |
[Node.js] 2. Express 프레임워크 (0) | 2018.03.27 |
이제 지금까지 배운 것을 통해서 Express 프레임워크를 통해 간단한 웹 애플리케이션을 만들어보자.
기존의 app.js에 추가하면 가독성이 떨어지므로, 프로젝트에 새로 app_file.js 파일을 만들어 진행하자.
이번 애플리케이션 실습을 통해 파일 시스템 모듈도 활용할 것이다.
데이터 값을 저장할 data 폴더, html로 나타낼 pug 파일을 저장할 views_file 폴더를 만들자.
이제 app_file.js를 작성해보도록 하자
1 2 3 4 5 6 7 | var express = require('express'); var fs = require('fs'); // 파일 시스템 모듈 생성 var app = express(); // 애플리케이션 객체 생성 app.listen(3000, function(){ console.log('Connected, 3000 port!'); }) | cs |
기존의 express를 require하고 app 변수로 express 객체를 생성하는 것에 추가로 fs 모듈을 생성한다.
listen을 통해 포트 번호 3000으로 접속시킨 기본 틀을 잡았다.
set을 통해 views에 있는 pug 파일을 사용할 수 있도록 선언하자.
1 2 | app.set('views', './views_file'); app.set('view engine', 'pug'); | cs |
1 2 3 4 5 6 7 8 9 10 11 12 | doctype html html head meta(charset='utf-8') body form(action='/topic' method='post') p input(type='text' name='title' placeholder='title') p textarea(name='description') p input(type='submit') | cs |
1 2 3 | app.get('/topic/new', function(req, res){ res.render('new'); }); | cs |
/topic이라는 path에 post를 이용해서 제목과 내용을 전송하는 코드를 작성해보자.
1 2 3 4 5 6 7 8 9 10 11 | app.post('/topic', function(req, res){ var title = req.body.title; var description = req.body.description; fs.writeFile('data/'+title, description, function(err){ if(err){ console.log(err); res.status(500).send('Internal Server Error'); } res.send('Success!'); }) }) | cs |
1 2 3 | var bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: false })) | cs |
1 2 3 4 5 6 7 8 9 | app.get('/topic', function(req,res){ fs.readdir('data', function(err, files){ if(err){ console.log(err); res.status(500).send('Internal Server Error'); } res.render('view', {topics:files}); }) }); | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | doctype html html head meta(charset='utf-8') body h1 a(href='/topic') Server Side JavaScript ul each topic in topics li a(href='/topic/'+topic)= topic article h2= title h4= description | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | app.get('/topic/:id', function(req, res){ var id = req.params.id; fs.readdir('data', function(err, files){ if(err){ console.log(err); res.status(500).send('Internal Server Error'); } fs.readFile('data/'+id, 'utf8', function(err, data){ if(err){ console.log(err); res.status(500).send('Internal Server Error'); } res.render('view', {topics:files, title:id, description:data}); }) }) }) | cs |
1 2 3 4 5 6 7 8 9 | app.get('/topic/new', function(req, res){ fs.readdir('data', function(err, files){ if(err){ console.log(err); res.status(500).send('Internal Server Error'); } res.render('new', {topics:files}); }) }) | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | doctype html html head meta(charset='utf-8') body h1 a(href='/topic') Server Side JavaScript ul each topic in topics li a(href='/topic/'+topic)= topic article form(action='/topic' method='post') p input(type='text' name='title' placeholder='title') p textarea(name='description') p input(type='submit') | cs |
1 2 | div a(href='/topic/new') new | cs |
1 2 3 | app.get(['/topic', '/topic/:id'], function(req,res){ ... }) | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | app.get(['/topic', '/topic/:id'], function(req,res){ fs.readdir('data', function(err, files){ if(err){ console.log(err); res.status(500).send('Internal Server Error'); } var id = req.params.id; if(id){ //id값이 있을 때 fs.readFile('data/'+id, 'utf8', function(err, data){ if(err){ console.log(err); res.status(500).send('Internal Server Error'); } res.render('view', {topics:files, title:id, description:data}); }) } else{ //id값이 없을 때 res.render('view', {topics:files, title:'Welcome', description:'Hello, JavaScript for server.'}); } }) }) | cs |
'Node.js' 카테고리의 다른 글
[Node.js] 핵심 요약 (0) | 2018.09.26 |
---|---|
[Node.js] 6. 파일 업로드 (0) | 2018.04.04 |
[Node.js] 4. Express 프레임워크 (get과 post) (0) | 2018.04.02 |
[Node.js] 3. Express 프레임워크(템플릿, URL) (0) | 2018.04.02 |
[Node.js] 2. Express 프레임워크 (0) | 2018.03.27 |