Created
September 14, 2016 14:31
-
-
Save onionmk2/9420a8ac00f7c9e0b274d4a7d8706e0f to your computer and use it in GitHub Desktop.
react_tutorial_server.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const Http = require('http'); | |
| const Url = require('url'); | |
| const Path = require('path'); | |
| const Fs = require('fs'); | |
| const COMMENTS_FILE = 'comments.json'; | |
| const server = Http.createServer(function requestListener(inComingMessage, serverResponse) { | |
| console.log(inComingMessage.method, inComingMessage.url, `at ${new Date()}`); | |
| console.log(inComingMessage.headers); | |
| function errorHandler(err, { side = 'client' } = {}) { | |
| console.error(err); | |
| const headerStatus = { | |
| clientError: { code: 404, name: 'NotFound' }, | |
| serverError: { code: 500, name: 'Internal Server Error' }, | |
| }; | |
| const header = { | |
| ContentType: 'application/json', | |
| }; | |
| if (side === 'client') { | |
| serverResponse.writeHead(headerStatus.clientError.code, headerStatus.clientError.name, header); | |
| } else { | |
| serverResponse.writeHead(headerStatus.serverError.code, headerStatus.serverError.name, header); | |
| } | |
| serverResponse.end(); | |
| } | |
| function sendComments() { | |
| const header = { | |
| 'ContentType': 'application/json', | |
| 'Access-Control-Allow-Origin': 'http://localhost:63342', | |
| 'Cache-Control': 'no-cache', | |
| }; | |
| serverResponse.writeHead('200', 'OK', header); | |
| const filePath = Path.join(__dirname, COMMENTS_FILE); | |
| Fs.readFile(filePath, 'utf-8', (err, data) => { | |
| if (err) { | |
| errorHandler(err, { side: 'server' }); | |
| } else { | |
| serverResponse.write(data); | |
| serverResponse.end(); | |
| } | |
| }); | |
| } | |
| function getCommentsHandler() { | |
| sendComments(); | |
| } | |
| function postCommentsHandler() { | |
| inComingMessage.setEncoding('utf8'); | |
| inComingMessage.on('data', (requestBody) => { | |
| const filePath = Path.join(__dirname, COMMENTS_FILE); | |
| Fs.readFile(filePath, (err, existingComments) => { | |
| if (err) { | |
| console.error(err); | |
| process.exit(1); | |
| } | |
| const comments = JSON.parse(existingComments); | |
| const newComment = JSON.parse(requestBody); | |
| comments.push(newComment); | |
| /* | |
| nodeではreadのcallback中にwriteを書いていいらしい。 | |
| fs.readFile(thePath, () => { | |
| fs.writeFile(thePath, () => { | |
| } | |
| }) | |
| */ | |
| const writeStream = Fs.createWriteStream(filePath); | |
| writeStream.write(JSON.stringify(comments)); | |
| writeStream.end(); | |
| sendComments(); | |
| }); | |
| }); | |
| } | |
| (function router() { | |
| const requestPathName = Url.parse(inComingMessage.url).pathname; | |
| const requestMethod = inComingMessage.method; | |
| if (requestPathName === '/api/comments') { | |
| switch (requestMethod) { | |
| case 'GET': | |
| getCommentsHandler(); | |
| break; | |
| case 'POST': | |
| postCommentsHandler(); | |
| break; | |
| default: | |
| errorHandler(new Error(`requestMethod: ${requestMethod}, not allowed`)); | |
| } | |
| } else { | |
| errorHandler(new Error(`requestPathName: ${requestPathName}, not implemented`)); | |
| } | |
| })(); | |
| }); | |
| server.listen(3000, function logger() { | |
| console.log('start server at localhost:3000'); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment