h1. Creating Veery API NodeJS feb 9, 2017 http://veeryclientnodejs.soupmode.com/ http://jothut.com/cgi-bin/junco.pl/blogpost/15807/GitHub-Commits https://github.com/jrsawvel?tab=repositories https://github.com/jrsawvel/Veery-API-Perl/blob/master/lib/App/Login.pm https://github.com/jrsawvel/Veery-API-Perl/blob/master/docs/veery-api-short-version.md https://github.com/jrsawvel/Veery-Client-NodeJS/blob/master/js/pageglobals.js https://www.npmjs.com/package/mailgun-js hr. Jan 24, 2017 http://veeryapinodejs.soupmode.com hr. edit dns. create cname record: hostname = veeryapinodejs is an alias of = @ cd into /home/veery mkdir Veery-API-NodeJS cd into Veery-API-NodeJS mkdir js cd js mkdir node_modules, although it might get automatically created later create package.json { "name" : "Veery-API-NodeJS", "version" : "1.0.0", "dependencies" : { "express" : "4.14.0", "body-parser" : "1.16.0", "html-entities" : "1.2.0", "querystring" : "0.2.0" } } unsure if i need all of those packages. i looked up the most recent package version numbers at https://www.npmjs.com the command "npm install" will read the package.json file, create node_modules directory, and place the downloaded the packages listed in the json file in that directory. within the same "js" directory, create dispatch.js var express = require('express'), bodyParser = require('body-parser'), path = require('path'), app = express(); app.use(bodyParser.urlencoded({ extended: true })); // parse application/x-www-form-urlencoded app.get('/', function(req, res){ res.send('hello world'); }); app.listen(3002); cd /etc/nginx/sites-available create veeryapinodejs.soupmode.com # veeryapinodejs.soupmode.com-info server { listen 80; server_name veeryapinodejs.soupmode.com; location / { default_type text/html; error_page 404 = @fallback; } location @fallback { proxy_pass http://veeryapinodejs.soupmode.com:3002; } } ln -s /etc/nginx/sites-available/veeryapinodejs.soupmode.com /etc/nginx/sites-enabled/veeryapinodejs.soupmode.com service nginx restart forever start dispatch.js more about forever commands: usage: forever [start | stop | stopall | list] [options] SCRIPT [script options] options: start start SCRIPT as a daemon stop stop the daemon SCRIPT stopall stop all running forever scripts list list all running forever scripts forever list in browser or with curl, access http://veeryapinodejs.soupmode.com should see the plain text response: hello world h3. etc. http://expressjs.com/en/4x/api.html http://jothut.com/cgi-bin/junco.pl/blogpost/36422/10Nov2014/Learning-NodeJS-Programming h3. couchdb clients http://wern-ancheta.com/blog/2015/04/26/getting-started-with-couchdb-in-node-dot-js/ https://www.terlici.com/2015/04/28/couchdb-node-express.html 1. https://www.npmjs.com/package/couchdb 2. https://www.npmjs.com/package/nano https://tanzimsaqib.wordpress.com/2015/05/25/crud-with-couchdb-in-node-js/ 3. https://www.npmjs.com/package/couchdb-client 4. - the one that i chose to try first: https://www.npmjs.com/package/couchdbjs h3. Jan 26, 2017 created command prompt test script. tried running but i received: SyntaxError: Use of const in strict mode. which is due to the use of const in the couchdbjs module and due to my server running and old version of node. http://askubuntu.com/questions/426750/how-can-i-update-my-nodejs-to-the-latest-version sudo npm cache clean -f sudo npm install -g n sudo n stable sudo ln -sf /usr/local/n/versions/node//bin/node /usr/bin/node version=7.4.0 but when i ran node -v, i was still running the old version instead of v7.4.0 that i installed. the old version was v0.10.21 i tried whereis node which produced: /usr/bin/node /usr/local/bin/node both of those were version 7.4.0. then i did "which node" which produced: /root/local/bin/node i did: ln -sf /usr/local/n/versions/node/7.4.0/bin/node /root/local/bin/node and now when i run "node -v", i get: v7.4.0 and now my test script works. test.js: 'use strict'; const Couchdbjs = require('couchdbjs'); const db = new Couchdbjs('mycouchdbname', cb); var docinfo = db.getDoc('profile', cb); var json_str = JSON.stringify(docinfo); function cb (err, data) { if (err) console.error(err); else console.log(data); // data is array of uuids } json= { _id: 'profile', _rev: '11-501aa2dd05bf45d71beee85c15bf2cca', author: 'JohnR', reading_time: 0, text_intro: 'My profile page. Nothing here for now.', created_at: '2015/06/12 14:04:52', html: '

My profile page.

\n\n

Nothing here for now.

\n', post_type: 'article', markup: 'h1. Profile\r\n\r\nMy profile page.\r\n\r\nNothing here for now.', more_text_exists: 0, tags: [], post_status: 'public', updated_at: '2015/09/15 22:51:42', word_count: 7, type: 'post', title: 'Profile' } h3. Helpful Links * https://nodejs.org/api/errors.html * http://veeryclientnodejs.soupmode.com/ * http://veeryapiperl.soupmode.com/api/posts/profile * http://veeryapiperl.soupmode.com/api/posts/profile/?text=markup * https://github.com/jrsawvel/Veery-API-Perl/blob/master/docs/veery-api.md * https://github.com/jrsawvel/Veery-Client-Perl/blob/master/lib/App/ShowPost.pm * https://github.com/jrsawvel/Veery-API-Perl/blob/master/lib/App/ReadPost.pm * https://github.com/jrsawvel/Veery-Client-NodeJS * http://jothut.com/cgi-bin/junco.pl/blogpost/15807/27Jan2017/GitHub-Commits * https://erichonorez.wordpress.com/2013/02/10/how-create-a-rest-api-with-node-js-and-express/ h3. Jan 27, 2017 forever info. i need to rename the dispatch.js files to something unique. i cannot determine which dispatch.js belongs to which app. i have to use ps and forever to match up files with apps. # date Fri Jan 27 17:04:04 UTC 2017 root@soupmode:/home/veery/Veery-Client-NodeJS/js# forever list info: Forever processes running data: uid command script forever pid logfile uptime data: [0] xX4P /root/local/bin/node dispatch.js 2156 9574 /root/.forever/xX4P.log 46:15:23:36.471 data: [1] RalR /root/local/bin/node index.js 3495 3499 /root/.forever/RalR.log 524:20:8:21.26 data: [2] v76p /root/local/bin/node dispatch.js 27383 24934 /root/.forever/v76p.log 367:22:57:58.595 no shock that after upgrading nodejs this week that now after stopping and restarting my dispatch.js files for older nodejs apps, those apps don't restart. i renamed the node_modules directory within my client nodejs app for veery to something old. then i cat package.json and i did nmp install for all of the packages. i also installed express-generator for some reason. anyway, i restarted dispatch.js and it didn't error out. my code works. h3. api code issues http://stackoverflow.com/questions/26066785/proper-way-to-set-response-status-and-json-content-in-a-rest-api-made-with-nodej http://expressjs.com/4x/api.html#res.send h3. Forever jan 27, 2017 unlink /root/local/bin/forever npm uninstall forever -g npm install forever -g ln -s /usr/local/n/versions/node/7.4.0/lib/node_modules/forever/bin/forever /root/local/bin/forever i don't know why the server environment wants to find forever at /root/local/bin/ i didn't read this but maybe it's useful http://garrows.com/blog/install-and-setup-node-js-to-run-forever/ h3. callbacks and promises https://www.tutorialspoint.com/nodejs/nodejs_callbacks_concept.htm http://stackoverflow.com/questions/29447451/user-defined-function-with-callback-in-nodejs http://stackoverflow.com/questions/18382186/creating-callbacks-for-required-modules-in-node-js http://callbackhell.com/ http://www.theprojectspot.com/tutorial-post/nodejs-for-beginners-callbacks/4 http://thenodeway.io/posts/understanding-error-first-callbacks/ http://www.theprojectspot.com/tutorial-post/Node-js-for-beginners-part-1-hello-world/2 https://www.codementor.io/codeforgeek/manage-async-nodejs-callback-example-code-du107q1pn https://medium.freecodecamp.com/javascript-callbacks-explained-using-minions-da272f4d9bcd#.6k7bcjxg4 https://docs.nodejitsu.com/articles/HTTP/clients/how-to-create-a-HTTP-request/ [x] https://www.codeschool.com/discuss/t/how-do-i-return-a-value-from-my-node-js-module-after-it-has-been-updated-by-an-async-function/27392 https://www.tomas-dvorak.cz/posts/nodejs-request-without-dependencies/ http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron https://blog.domenic.me/youre-missing-the-point-of-promises/ https://developers.google.com/web/fundamentals/getting-started/primers/promises [x] http://stackoverflow.com/questions/10058814/get-data-from-fs-readfile/10058879#10058879 function doSomething (callback) { // any async callback invokes callback with response } doSomething (function doSomethingAfter(err, result) { // process the async result }); [x] http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323 [x] http://stackoverflow.com/questions/23339907/returning-a-value-from-callback-function-in-node-js [x] http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call h3. bodyparser json parsing http://stackoverflow.com/questions/24330014/bodyparser-is-deprecated-express-4 h3. looping through object https://coderwall.com/p/_kakfa/javascript-iterate-through-object-keys-and-values http://stackoverflow.com/questions/5829007/looping-through-json-with-node-js http://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript http://stackoverflow.com/questions/29397746/in-nodejs-is-there-a-way-to-loop-through-an-array-without-using-array-size https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys h3. PUT Requests always something ... https://github.com/cloudfoundry/nodejs-buildpack/issues/57 http://stackoverflow.com/questions/22231109/nginx-returns-405-method-not-allowed-for-put-or-delete and no answers. i can do post and get requests but put request fails at nginx with 405 returned. http://stackoverflow.com/questions/24415376/post-request-not-allowed-405-not-allowed-nginx-even-with-headers-included