# 5/17 高レイヤ勉強会
## 22
### handler-util.js
```
'use strict';
function handleLogout(req, res) {
res.writeHead(401, {
'Content-Type': 'text/plain; charset=utf-8'
});
res.end('ログアウトしました');
}
function handleNotFound(req, res) {
res.writeHead(404, {
'Content-Type': 'text/plain; charset=utf-8'
});
res.end('ページがみつかりません');
}
module.exports = {
handleLogout,
handleNotFound
};
```
### posts-handler.js
```
'use strict';
const pug = require('pug');
const contents = [];
function handle(req, res) {
switch (req.method) {
case 'GET':
res.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8'
});
res.end(pug.renderFile('./views/posts.pug', { contents: contents }));
break;
case 'POST':
let body = [];
req
.on('data', chunk => {
body.push(chunk);
})
.on('end', () => {
body = Buffer.concat(body).toString();
const decoded = decodeURIComponent(body);
const content = decoded.split('content=')[1];
console.info('投稿されました: ' + content);
contents.push(content);
console.info('投稿された全内容: ' + contents);
handleRedirectPosts(req, res);
});
break;
default:
break;
}
}
function handleRedirectPosts(req, res) {
res.writeHead(303, {
Location: '/posts'
});
res.end();
}
module.exports = {
handle
};
```
# 23
```
sudo apt install -y postgresql-10
```
### database drop
```
sudo su - postgres
psql
drop database secret_board;
create database secret_board;
```