almost 4 years ago

有了前面基礎的環境建置背景知識了解後,
以官方的evernote-sdk-js為基礎,
就來練習第一個Evernote API的應用~建立筆記,也就是CRUD的Create

流程:

  • 進到首頁,先連結Evernote帳號
  • 進入Create Note頁面
  • 輸入標題、內容,按下Create Note //create
  • 在Evernote建立筆記,導回首頁 //receive

首頁畫面

views/index.jade
hr
  - if(session.notebooks)
    h2 Notebooks
    ul
      - for (var i = 0; i < session.notebooks.length; ++i)
        li= session.notebooks[i].name
    hr
  h2 Create Note
  p
    a(href='/create') Click here
    |  to
    | create note
   hr 

創建筆記畫面

views/create.html
<html>
  <head>
      <title>Create Note</title>
  </head>
  <body>
    <form action='/receive' method='post'> 
      <a>Title:</a>
        <input type='text' name='title'> 
      <br>
      <a>Content:</a>
        <textarea name='content' cols="50" rows="5"></textarea>
      <br>
      <input type='submit' value='Create note'> 
    </form>
  </body>
</html>
<script>
  $.post("ask", { content: $("#content").val() });
</script>

路徑導向

app.js
app.get('/create', routes.create);
app.post('/receive',routes.receive);

主程式內容

routes/index.js
var token = null;
var client = null;
var oauth = false;

exports.create = function(req, res) {
    if(oauth) {
        res.sendfile("views/create.html");
    }else{
        res.redirect('/');
    }
    
};

exports.receive = function(req, res) {
    var myBody = req.body.content;
    var nBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    nBody += "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">";
    nBody += "<en-note>" + myBody + "</en-note>";
 
    var ourNote = new Evernote.Note();
    ourNote.title = req.body.title;
    console.log("Create note:" + ourNote.title);
    ourNote.content = nBody;
    console.log("Content:" + nBody);
    
    var noteStore = client.getNoteStore();
    noteStore.createNote(ourNote, function(err, note) {
        if (err) {
            console.log(err);
        } else {
            //callback(note);

        }
    });

    console.log("create success");
    res.redirect('/');
};

Demo

terminal執行 node app.js,瀏覽器開啟你的koding domain:3000
先連結Sandbox的帳號:Authentication




連結&授權成功會看到Sandbox帳號有的筆記本,以及其他一些資訊

連結Sandbox的帳號後就可以Create Node,輸入想要的筆記標題、內容

登入Sandbox,就可以看到新建立的筆記囉!

參考資料

Evernote Developer - Create Node ( 簡中版 )
淺談 HTTP Method:表單中的 GET 與 POST 有什麼差別?

← Evernote API - 背景知識:Node.js、MVC、express Evernote API - 接下來的應用 (Node.js with express) →
 
comments powered by Disqus