@@ -5,6 +5,8 @@ var util= require('util') |
5 | 5 | var querystring = require('querystring') |
6 | 6 | // Loads querystring module, we'll need it to serialize and deserialize objects and query strings |
7 | 7 |
|
| 8 | +var port = process.env.PORT || 1337 |
| 9 | + |
8 | 10 | var messages=[] |
9 | 11 | // This array will hold our messages |
10 | 12 | messages.push({ |
@@ -17,13 +19,12 @@ exports.server=http.createServer(function (req, res) { |
17 | 19 | // Creates server |
18 | 20 | if (req.method == 'POST' && req.url == '/messages/create.json') { |
19 | 21 | // If method is POST and URL is messages/ add message to the array |
20 | | - var message='' |
| 22 | + var message = '' |
21 | 23 | req.on('data', function(data, msg){ |
22 | 24 | console.log(data.toString('utf-8')) |
23 | 25 | message=exports.addMessage(data.toString('utf-8')) |
24 | 26 | // Data is type of Buffer and must be converted to string with encoding UTF-8 first |
25 | 27 | // Adds message to the array |
26 | | - |
27 | 28 | }) |
28 | 29 | req.on('end', function(){ |
29 | 30 | console.log('message', util.inspect(message, true, null)) |
@@ -37,25 +38,23 @@ exports.server=http.createServer(function (req, res) { |
37 | 38 | } else |
38 | 39 | if (req.method == 'GET' && req.url == '/messages/list.json') { |
39 | 40 | // If method is GET and URL is /messages output list of messages |
40 | | - var body=exports.getMessages() |
| 41 | + var body = exports.getMessages() |
41 | 42 | // Body will hold our output |
42 | 43 | res.writeHead(200, { |
43 | 44 | 'Content-Length': body.length, |
44 | 45 | 'Content-Type': 'text/plain' |
45 | 46 | }) |
46 | 47 | res.end(body) |
47 | | - } |
48 | | - else { |
| 48 | + } else { |
49 | 49 | res.writeHead(200, {'Content-Type': 'text/plain'}) |
50 | 50 | // Sets the right header and status code |
51 | 51 | res.end('Hello World\n') |
| 52 | + // Outputs string with line end symbol |
52 | 53 | } |
53 | | - console.log(req.method) |
54 | 54 |
|
55 | | - // Outputs string with line end symbol |
56 | | -}).listen(process.env.PORT || 1337) |
| 55 | +}).listen(port) |
57 | 56 | // Sets port and IP address of the server |
58 | | -console.log('Server running at http://127.0.0.1:1337/') |
| 57 | +console.log('Server running at http://127.0.0.1:%s/', port) |
59 | 58 |
|
60 | 59 |
|
61 | 60 | exports.getMessages = function() { |
|