nodejs ๋…ํ•™ 3

[์ƒํ™œ์ฝ”๋”ฉ] Node.js ๋ฅผ ์ด์šฉํ•ด ์›น์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งŒ๋“ค๊ธฐ(4)

๊ธ€ ๋ชฉ๋ก ๋งŒ๋“ค๊ธฐ ์ €์žฅ๋œ data๋ฅผ ์ฝ์–ด์˜ค๋Š” ๋ฒ• *GET์— ๋Œ€ํ•œ ๋ผ์šฐํŠธ app.get('/topic', function(req, res){ // get ๋ฐฉ์‹์œผ๋กœ /topic ๋ผ์šฐํŠธ fs.readdir('data', function(err, files){ if(err){ res.status(500).send('Internal Server Error'); } res.render('view', {topics: files}); }) }) ํ…œํ”Œ๋ฆฟ์—”์ง„์„ ์ด์šฉํ•œ๋‹ค app.set('views', './views_file'); app.set('view engine', 'pug'); ์ „ ์ˆ˜์—…์—์„œ ์ž‘์„ฑํ–ˆ๋˜ ์ด ์ฝ”๋“œ๊ฐ€ ์žˆ์–ด์•ผ ํ•จ readdir์„ ์ด์šฉํ•ด์„œ data๋ฅผ ์ฝ์–ด์˜จ๋‹ค ๊ทธ๋•Œ callback ํ•จ์ˆ˜๋Š” err์™€ file ๋ชฉ๋ก(f..

[์ƒํ™œ์ฝ”๋”ฉ] Node.js ๋ฅผ ์ด์šฉํ•ด ์›น์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งŒ๋“ค๊ธฐ(3)

์ฟผ๋ฆฌ์ŠคํŠธ๋ง http://a.com/topic?id=1 protocol / domain / path / query string ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์— ๋”ฐ๋ผ ๊ฐ™์€ path์—์„œ๋„ ๋‹ค๋ฅธ ํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋‹ค app.get('/topic', function(req, res){ let topics = [ 'Javascript is...', 'Nodejs is...', 'Express is...' ]; let str = ` JavaScript Nodejs Express ` let output = str + topics[req.query.id]; res.send(output); }); aํƒœ๊ทธ๋ฅผ ์ด์šฉํ•ด์„œ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์„ ๋ฐ”๊ฟ”์ค€๋‹ค ๋”๋ณด๊ธฐ app.get('/topic',function(req, res){ let topics = [ '..

[์ƒํ™œ์ฝ”๋”ฉ] Node.js ๋ฅผ ์ด์šฉํ•ด ์›น์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งŒ๋“ค๊ธฐ(2)

๋™๊ธฐํ™” ๋น„๋™๊ธฐํ™” let fs = require('fs'); // Sync console.log('1'); let data = fs.readFileSync('data.txt',{encoding:'utf8'}); console.log(data); //Async console.log('2'); fs.readFile('data.txt', {encoding:'utf8'}, function(err, data){ console.log('3'); console.log(data); }); console.log('4'); Express express๋ฅผ ์„ค์น˜ํ•œ ํ›„! let express = require('express'); let app = express(); app.get('/', function(req, res){ r..