CLI와 HATEOAS를 결합한 Todo 목록

원래:

app.get('/notes', (req, res) => {
  const rawNotesList = fs.readFileSync('../cli/notes.json', {encoding: 'utf8', flag: 'r'});
  const parsedNotesList = JSON.parse(rawNotesList);
  const links = [
    {
      rel: ["self"],
      href: [baseUrl, '/notes'].join('')
    }
  ];

  const actions = [
    {
      class: "add-note",
      href: [baseUrl, '/notes'].join(''),
      method: 'POST',
      fields: [
        {
          name: "command",
          type: "string"
        },
        {
          name: "content",
          type: "string"
        }
      ]
    }
  ];

  const responseWithControlData = {
    class: "note",
    ...parsedNotesList,
    links: links,
    actions: actions

  };

  res.json(responseWithControlData);
});


그리고:

app.post('/notes', (req, res) => {

  const body = req.body;

  if (!body.command.includes('add')) {
    return res.status(400).json({
      status: 'failed to add content', 
      content: body.content
    });
  }

  const cliCapture = new CliCapture({
    notesPath: './notes.json',
    command: body.command,
    content: body.content
  });

  res.json({
    status: 'added from server'
  });

});


우리는 이 CoAP/CoRE 및 Amundsen의 DARRT 접근 방식을 유추할 수 있습니다. "CliCapture"를 실제로 대체할 수 있는 모든 것과 fs를 사용할 수 있는 곳이라면 어디에서나 리소스 디렉터리를 HTTP( https://github.com/JelmerT/coap-rd/blob/master/requesthandlers.js#L82 )에 매핑하여 기능을 확장할 수 있습니다.



https://github.com/nerdfiles/notes-cli/blob/master/cli/server/index.js#L19-L56https://github.com/nerdfiles/notes-cli/blob/master/cli/server/index.js#L69-L73 참조

좋은 웹페이지 즐겨찾기