<p class="p1"><!DOCTYPE <span class="s1">html</span>></p>
<p class="p1"><span class="s1"><html></span></p>
<p class="p1"><head></p>
<p class="p1"><meta <span class="s1">charset</span>="UTF-8"></p>
<p class="p1"><title><%=title %></title></p>
<p class="p1"><link type="text.css" <span class="s1">href</span>="style.css" <span class="s1">rel</span>="<span class="s1">stylesheet</span>"></p>
<p class="p1"></head></p>
<p class="p1"><body></p>
<p class="p1"><h1><%=title %></h1></p>
<p class="p1"><p><%=<span class="s1">msg</span> %></p>
<p class="p1"></body></p>
&lt;p class=&quot;p1&quot;&gt;&amp;lt;/html&amp;gt;&lt;/p&gt;</p>

<p>&lt;h3&gt;node.js&lt;/h3&gt;
var http = require('http');
var fs = require('fs');
var url = require('url');
var ejs = require('ejs');
var cookie = require('cookie');</p>

<p>var index = fs.readFileSync('./index.ejs','utf8');
var style = fs.readFileSync('./style.css','utf8');
var server = http.createServer();</p>

<p>server.on('request',doRequest);
server.listen(1337);</p>

<p>function doRequest(req,res){
var path = url.parse(req.url);
switch(path.pathname){
case '/' :
var msg = &quot;クッキーはありません&quot;;
if(req.headers.cookie != null){
var ck = cookie.parse(req.headers.cookie);
msg = &quot;クッキー:&quot; + ck.lasturl + &quot;,&quot; + 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;</p>

<p>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 http://127.0.0.1:1337');</p>

<p>

(Visited 1 times, 1 visits today)

コメントを残す

メールアドレスが公開されることはありません。