<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><%=title %></title>
<link type="text.css" href="style.css" rel="stylesheet">
</head>
<body>
<h1><%=title %></h1>
<p><%=msg %>
</body>
</html>
node.js
var http = require('http');
var fs = require('fs');
var url = require('url');
var ejs = require('ejs');
var cookie = require('cookie');
var index = fs.readFileSync('./index.ejs','utf8');
var style = fs.readFileSync('./style.css','utf8');
var server = http.createServer();
server.on('request',doRequest);
server.listen(1337);
function doRequest(req,res){
var path = url.parse(req.url);
switch(path.pathname){
case '/' :
var msg = "クッキーはありません";
if(req.headers.cookie != null){
var ck = cookie.parse(req.headers.cookie);
msg = "クッキー:" + ck.lasturl + "," + ck.lasttime;
}
var tmp = ejs.render(index,{
title : 'index Page',
msg : msg
});
res.setHeader('Content-Type','text/html');
res.write(tmp);
res.end();
break;
case '/style.css':
res.setHeader('Content-Type','text/css');
res.write(style);
res.end();
break;
case '/time':
var d = new Date().toDateString();
var ck1 = cookie.serialize('lasttime',d,{
maxAge : 100
});
res.setHeader('Set-Cookie',ck1);
res.setHeader('Content-Type','text/plain');
res.write('SET - COOO_KIEE!');
res.end();
break;
case '/favicon.ico':
break;
default :
res.setHeader('Content-Type','text/plain');
res.setHeader('Set-Cookie',['lasturl=' + path.pathname]);
res.write('SET COOKIE!');
res.end();
break;
}
}
console.log('Server runnning at https://127.0.0.1:1337');