forked from reactjs/react-tutorial
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.php
More file actions
Latest commit
41 lines (36 loc) · 1.2 KB
/
Copy pathserver.php
File metadata and controls
41 lines (36 loc) · 1.2 KB
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
<?php
$scriptInvokedFromCli =
isset($_SERVER['argv'][0]) && $_SERVER['argv'][0] === 'server.php';
if($scriptInvokedFromCli) {
$port = getenv('PORT');
if (empty($port)) {
$port = "3000";
}
echo'starting server on port '. $port . PHP_EOL;
exec('php -S localhost:'. $port . ' -t public server.php');
} else {
returnrouteRequest();
}
functionrouteRequest()
{
$comments = file_get_contents('comments.json');
switch($_SERVER["REQUEST_URI"]) {
case'/':
echofile_get_contents('./public/index.html');
break;
case'/comments.json':
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$commentsDecoded = json_decode($comments, true);
$commentsDecoded[] = ['author' => $_POST['author'],
'text' => $_POST['text']];
$comments = json_encode($commentsDecoded, JSON_PRETTY_PRINT);
file_put_contents('comments.json', $comments);
}
header('Content-Type: application/json');
header('Cache-Control: no-cache');
echo$comments;
break;
default:
returnfalse;
}
}