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
|
var http = require('http') var fs = require('fs') var url = require('url') var template = require('art-template')
var comments = [ { name: '张三', message: '今天天气不错!', dateTime: '2015-10-16' }, { name: '张三2', message: '今天天气不错!', dateTime: '2015-10-16' }, { name: '张三3', message: '今天天气不错!', dateTime: '2015-10-16' }, { name: '张三4', message: '今天天气不错!', dateTime: '2015-10-16' }, { name: '张三5', message: '今天天气不错!', dateTime: '2015-10-16' } ]
http .createServer(function (req, res) { var parseObj = url.parse(req.url, true)
var pathname = parseObj.pathname
if (pathname === '/') { fs.readFile('./views/index.html', function (err, data) { if (err) { return res.end('404 Not Found.') } var htmlStr = template.render(data.toString(), { comments: comments }) res.end(htmlStr) }) } else if (pathname === '/post') { fs.readFile('./views/post.html', function (err, data) { if (err) { return res.end('404 Not Found.') } res.end(data) }) } else if (pathname.indexOf('/public/') === 0) { fs.readFile('.' + pathname, function (err, data) { if (err) { return res.end('404 Not Found.') } res.end(data) }) } else if (pathname === '/pinglun') {
var comment = parseObj.query comment.dateTime = '2017-11-2 17:11:22' comments.unshift(comment)
res.statusCode = 302 res.setHeader('Location', '/') res.end() } else { fs.readFile('./views/404.html', function (err, data) { if (err) { return res.end('404 Not Found.') } res.end(data) }) } }) .listen(3000, function () { console.log('running...') })
|