복붙노트

[REACTJS] 어떻게 로컬로 제공 JSON 파일의 웹 서비스 엔드 포인트를 만들 수 있습니다

REACTJS

어떻게 로컬로 제공 JSON 파일의 웹 서비스 엔드 포인트를 만들 수 있습니다

해결법


  1. 1.당신이 reactjs 보면 / 반응-튜토리얼 GitHub의에, Node.js를를 사용하는 예를 들어 서버가있다 :

    당신이 reactjs 보면 / 반응-튜토리얼 GitHub의에, Node.js를를 사용하는 예를 들어 서버가있다 :

    git clone git@github.com:reactjs/react-tutorial.git && cd react-tutorial
    npm install
    node server.js
    

    여기에 server.js 파일입니다.

    var fs = require('fs');
    var path = require('path');
    var express = require('express');
    var bodyParser = require('body-parser');
    var app = express();
    
    var comments = JSON.parse(fs.readFileSync('_comments.json'));
    
    app.use('/', express.static(path.join(__dirname, 'public')));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended: true}));
    
    app.get('/comments.json', function(req, res) {
      res.setHeader('Content-Type', 'application/json');
      res.send(JSON.stringify(comments));
    });
    
    app.post('/comments.json', function(req, res) {
      comments.push(req.body);
      res.setHeader('Content-Type', 'application/json');
      res.send(JSON.stringify(comments));
    });
    
    app.listen(3000);
    
    console.log('Server started: http://localhost:3000/');
    
    /**
     * This file provided by Facebook is for non-commercial testing and evaluation purposes only.
     * Facebook reserves all rights not expressly granted.
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
     * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
     * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     */
    
  2. from https://stackoverflow.com/questions/26064533/how-can-i-create-a-web-service-endpoint-for-a-locally-served-json-file by cc-by-sa and MIT license