# 5/6 高レイヤ勉強会
【2020年度】プログラミング入門 Webアプリ-サーバーサイドプログラミング入門 - N予備校
yarn入れるやつ
```
$ curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.21.1
$ source ~/.bashrc
```
```
https://github.com/progedu/yarn-training
```
yarn でパッケージをインストール
```
yarn add axios
```
use axios
```
# app.js
'use strict';
const axios = require('axios');
axios.get('https://www.google.com').then(res => {
console.log(res.data);
});
```
sum パッケージを作る
```
# index.js
'use strict';
function add(numbers) {
let result = 0;
for (let num of numbers) {
result = result + num;
}
return result;
}
module.exports = {
add: add
};
```
自分で作ったsumパッケージを使う
```
# app.js
'use strict';
const s = require('sum');
console.log(s.add([1, 2, 3, 4]));
```
## slackbot 開発は飛ばす
## 12 HTTPserver
node-js-http
```
// index.js
'use strict';
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, {
'Content-Type': 'text/plain; charset=utf-8'
});
res.write(req.headers['user-agent']);
res.end();
});
const port = 8000;
server.listen(port, () => {
console.log('Listening on ' + port);
});
```
# 13
ログを取る機能の実装
```
'use strict';
const http = require('http');
const server = http.createServer((req, res) => {
console.info(
'[' + new Date() + '] Requested by ' + req.connection.remoteAddress
);
res.writeHead(200, {
'Content-Type': 'text/plain; charset=utf-8'
});
res.write(req.headers['user-agent']);
res.end();
});
const port = 8000;
server.listen(port, () => {
console.log('Listening on ' + port);
});
```
エラー出力の実装
```
// index.js
'use strict';
const http = require('http');
const server = http
.createServer((req, res) => {
console.info(
'[' + new Date() + '] Requested by ' + req.connection.remoteAddress
);
res.writeHead(200, {
'Content-Type': 'text/plain; charset=utf-8'
});
res.write(req.headers['user-agent']);
res.end();
})
.on('error', e => {
console.error('[' + new Date() + '] Server Error', e);
})
.on('clientError', e => {
console.error('[' + new Date() + '] Client Error', e);
});
const port = 8000;
server.listen(port, () => {
console.log('Listening on ' + port);
});
```
# 14 HTTP method
post method 実装
```
// index.js
'use strict';
const http = require('http');
const server = http
.createServer((req, res) => {
const now = new Date();
console.info('[' + now + '] Requested by ' + req.connection.remoteAddress);
res.writeHead(200, {
'Content-Type': 'text/plain; charset=utf-8'
});
switch (req.method) {
case 'GET':
res.write('GET ' + req.url);
break;
case 'POST':
res.write('POST ' + req.url);
let rawData = '';
req
.on('data', chunk => {
rawData = rawData + chunk;
})
.on('end', () => {
console.info('[' + now + '] Data posted: ' + rawData);
});
break;
default:
break;
}
res.end();
})
.on('error', e => {
console.error('[' + new Date() + '] Server Error', e);
})
.on('clientError', e => {
console.error('[' + new Date() + '] Client Error', e);
});
const port = 8000;
server.listen(port, () => {
console.info('[' + new Date() + '] Listening on ' + port);
});
```
## 15 Making html form
```
//form.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<title>アンケート</title>
</head>
<body>
<h1>どちらが食べたいですか?</h1>
<form method="post" action="/enquetes/yaki-shabu">
<input type="radio" name="yaki-shabu" value="焼き肉" /> 焼き肉
<input type="radio" name="yaki-shabu" value="しゃぶしゃぶ" /> しゃぶしゃぶ
<button type="submit">投稿</button>
</form>
</body>
</html>
```
```
// index.js
'use strict';
const http = require('http');
const server = http
.createServer((req, res) => {
const now = new Date();
console.info('[' + now + '] Requested by ' + req.connection.remoteAddress);
res.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8'
});
switch (req.method) {
case 'GET':
const fs = require('fs');
const rs = fs.createReadStream('./form.html');
rs.pipe(res);
break;
case 'POST':
res.write('POST ' + req.url);
let rawData = '';
req
.on('data', chunk => {
rawData = rawData + chunk;
})
.on('end', () => {
console.info('[' + now + '] Data posted: ' + rawData);
res.end();
});
break;
default:
break;
}
})
.on('error', e => {
console.error('[' + new Date() + '] Server Error', e);
})
.on('clientError', e => {
console.error('[' + new Date() + '] Client Error', e);
});
const port = 8000;
server.listen(port, () => {
console.info('[' + new Date() + '] Listening on ' + port);
});
```
POSTデータをURLエンコードいてhtml表示
```
// index.js
'use strict';
const http = require('http');
const server = http
.createServer((req, res) => {
const now = new Date();
console.info('[' + now + '] Requested by ' + req.connection.remoteAddress);
res.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8'
});
switch (req.method) {
case 'GET':
const fs = require('fs');
const rs = fs.createReadStream('./form.html');
rs.pipe(res);
break;
case 'POST':
let rawData = '';
req
.on('data', chunk => {
rawData = rawData + chunk;
})
.on('end', () => {
const decoded = decodeURIComponent(rawData);
console.info('[' + now + '] 投稿: ' + decoded);
res.write('<!DOCTYPE html><html lang="ja"><body><h1>' + decoded + 'が投稿されました</h1></body></html>');
res.end();
});
break;
default:
break;
}
})
.on('error', e => {
console.error('[' + new Date() + '] Server Error', e);
})
.on('clientError', e => {
console.error('[' + new Date() + '] Client Error', e);
});
const port = 8000;
server.listen(port, () => {
console.info('[' + new Date() + '] Listening on ' + port);
});
```