-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata-server.coffee
46 lines (35 loc) · 1.21 KB
/
data-server.coffee
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
fs = require 'fs'
http = require 'http'
Mustache = require './server/mustache.js'
{ LineReader } = require './reader.js'
main = ->
docTitles = {}
reader = new LineReader 'server/titles.data'
while not reader.isEof()
line = reader.readLine()
if line
[pid, title] = line.split ':'
docTitles["#{pid}"] = title
reader.close()
jsonValue = fs.readFileSync 'server/topics.json'
topics = JSON.parse jsonValue
for i in [0...topics.length]
topic = topics[i]
topic.docInfo = []
for j in [0...topic.docs.length]
doc = topic.docs[j]
if doc of docTitles
title = docTitles[doc]
docLink =
content: "<a href=\"http://test.com/post/#{doc}\" target=\"_blank\">#{title}</a>"
topic.docInfo.push docLink
console.log 'Data inited.'
context =
topics: topics
server = http.createServer (req, res) ->
template = fs.readFileSync "server/index.html"
res.writeHead 200, { 'Content-Type': 'text/html' }
res.end Mustache.to_html "#{template}", context
server.listen 8000
console.log "Server is listening on 8000"
main()