그놈의 안드로이드
Node.js 웹서버 구성 본문
1. $ apt-get install curl
2. $ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
3. $ apt-get install -y nodejs
4. $ npm config get prefix -> npm 디렉토리 확인 /usr 일경우 바로 밑에 진행 ㄱㄱ
5. $ mkdir ~/npm-global-modules
6. $ npm config set prefix '~/npm-global-modules'
7. $ export PATH=~/npm-global-modules/bin:$PATH
8. $ source ~/.profile
9. $ npm install -S mysql (MySQL 설치)
#이미지는 /usr/image 안에
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | var http = require('http'); var fs = require('fs'); var url = require('url'); var qs = require('qs'); var template = require('./template.js'); var path = require('path'); var imageDir = '/usr/image/'; var mysql = require('mysql'); var db = mysql.createConnection({ host : 'localhost', user : 'root', password : '1234', database : 'tn' }); db.connect(); function getImages(imageDir, callback) { var fileType = '.jpg', files = [], i; fs.readdir(imageDir, function (err, list) { for(i=0; i<list.length; i++) { if(path.extname(list[i]) === fileType) { files.push(list[i]); //store the file name into the array files }else{ i++; } } callback(err, files); }); } var app = http.createServer(function(request, response){ var query = url.parse(request.url,true).query; pic= query.image; var _url = request.url; var queryData = url.parse(_url, true).query; var pathname = url.parse(_url, true).pathname; if(pathname === '/'){ if(queryData.id === undefined){ db.query('select * from tn', function(error, result){ var title = 'welcome'; var desc = 'hi node'; var list = template.list(result); var html; if (typeof pic === 'undefined') { getImages(imageDir, function (err, files) { var imageLists; html = template.HTML(title, list, '<h2>${title}</h2>${desc}', '<a href="/create"></a>' ); imageLists += html; response.writeHead(200, {'Content-type':'text/html'}); response.end(imageLists); }); }else { //read the image using fs and send the image content back in the response fs.readFile(imageDir + pic, function (err, content) { if (err) { response.writeHead(400, {'Content-type':'text/html'}) console.log(err); response.end("No such image"); } else { //specify the content type in the response will be an image response.writeHead(200,{'Content-type':'image/jpg'}); response.end(content); } }); } }); } } if(pathname === '/big'){ if(queryData.id === undefined){ db.query('select * from tn', function(error, result){ var title = 'welcome'; var desc = 'hi node'; var list = template.list(result); var html; if (typeof pic === 'undefined') { getImages(imageDir, function (err, files) { var imageLists = ''; imageLists += '<img src="/?image=' + files[0] + '">'; response.writeHead(200, {'Content-type':'text/html'}); response.end(imageLists); }); }else { //read the image using fs and send the image content back in the response fs.readFile(imageDir + pic, function (err, content) { if (err) { response.writeHead(400, {'Content-type':'text/html'}) console.log(err); response.end("No such image"); } else { //specify the content type in the response will be an image response.writeHead(200,{'Content-type':'image/jpg'}); response.end(content); } }); } }); } } if(pathname === '/small1'){ if(queryData.id === undefined){ db.query('select * from tn', function(error, result){ var title = 'welcome'; var desc = 'hi node'; var list = template.list(result); var html; if (typeof pic === 'undefined') { getImages(imageDir, function (err, files) { var imageLists = ''; imageLists += '<img src="/?image=' + files[1] + '">'; response.writeHead(200, {'Content-type':'text/html'}); response.end(imageLists); }); }else { //read the image using fs and send the image content back in the response fs.readFile(imageDir + pic, function (err, content) { if (err) { response.writeHead(400, {'Content-type':'text/html'}) console.log(err); response.end("No such image"); } else { //specify the content type in the response will be an image response.writeHead(200,{'Content-type':'image/jpg'}); response.end(content); } }); } }); } } if(pathname === '/small2'){ if(queryData.id === undefined){ db.query('select * from tn', function(error, result){ var title = 'welcome'; var desc = 'hi node'; var list = template.list(result); var html; if (typeof pic === 'undefined') { getImages(imageDir, function (err, files) { var imageLists = ''; imageLists += '<img src="/?image=' + files[2] + '">'; response.writeHead(200, {'Content-type':'text/html'}); response.end(imageLists); }); }else { //read the image using fs and send the image content back in the response fs.readFile(imageDir + pic, function (err, content) { if (err) { response.writeHead(400, {'Content-type':'text/html'}) console.log(err); response.end("No such image"); } else { //specify the content type in the response will be an image response.writeHead(200,{'Content-type':'image/jpg'}); response.end(content); } }); } }); } } }); app.listen(3000); | cs |
'리눅스 > 웹 서버' 카테고리의 다른 글
Tornado 웹서버 구성 (0) | 2018.12.30 |
---|---|
Vert.x 웹서버 구성 (0) | 2018.12.30 |
Netty 웹서버 구성 (0) | 2018.12.30 |
H2o 설치 (0) | 2018.12.30 |
Lighttpd (0) | 2018.12.30 |