-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (37 loc) · 1.29 KB
/
index.js
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
'use strict'
const connect = require ('connect')
const http = require ('http')
const serveStatic = require ('serve-static')
const requestBodyParser = require ('body-parser')
const store = [
{author: 'Pete Hunt', text: 'This is one comment.'},
{author: 'Jordan Walke', text: 'This is *another* comment.'}
]
const app = connect ()
app.use (serveStatic ('public', {index: 'index.html'}))
app.use (requestBodyParser.json ())
app.use ('/comments.json', function (request, response) {
response.setHeader ('Content-Type', 'application/json; charset=utf-8')
switch (request.method) {
case 'GET': {
response.end (JSON.stringify (store), 'utf-8')
break
}
case 'POST': {
const newComment = request.body;
store.push (newComment)
response.end (JSON.stringify (newComment), 'utf-8')
break
}
}
})
app.use (function (request, response) {
response.end ('This is the ES6 (2015) version of the official React Tutorial! Nothing is configured for this URI.\n')
})
const httpServerOptions = {
host: 'localhost',
port: 3000
}
http.createServer (app).listen (httpServerOptions, function () {
console.log (`HTTP server listening on ${httpServerOptions.host}:${httpServerOptions.port}...`)
})