-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.coffee
51 lines (34 loc) · 916 Bytes
/
app.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
47
48
49
50
51
path = require 'path'
express = require 'express'
bodyParser = require 'body-parser'
st = require 'st'
app = express()
http = require('http').Server(app)
io = require('socket.io')(http)
validateInput = require './src/shared/coffee/validator.coffee'
fs = require 'fs'
app.use bodyParser.json()
# Sockets
io.on 'connection', (socket) ->
socket.on 'word change', (word) ->
io.emit 'word change', word
# Some routes
wordFile = './word.txt'
app.get '/word', (req, res, next) ->
fs.readFile wordFile, 'utf8', (err, data) =>
word = data
res.send(word)
app.post '/word', (req, res, next) ->
body = req.body
validatedObj = validateInput(body.word)
return if validatedObj.errorEmptyWord
word = validatedObj.output
fs.writeFile wordFile, word, 'utf8'
# Static Content
mount = st
path: __dirname + '/public'
url: '/'
index: 'index.html'
app.use(mount)
# Start
http.listen(10060)