You're viewing old version number 35. - Current version

4 min

Creating Veery API NodeJS

Jan 24, 2017

http://veeryapinodejs.soupmode.com


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

  1. 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

etc.

http://expressjs.com/en/4x/api.html

http://jothut.com/cgi-bin/junco.pl/blogpost/36422/10Nov2014/Learning-NodeJS-Programming

couchdb clients

1.
https://www.npmjs.com/package/couchdb

2.
https://www.npmjs.com/package/nano

3.
https://www.npmjs.com/package/couchdb-client

4. - the one that i chose to try first:
https://www.npmjs.com/package/couchdbjs

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/<VERSION>/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: '<p>My profile page.</p>\n\n<p>Nothing here for now.</p>\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'
}

Helpful Links

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.

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

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/

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

bodyparser json parsing

http://stackoverflow.com/questions/24330014/bodyparser-is-deprecated-express-4

From JR's : articles
758 words - 8824 chars - 4 min read
created on
updated on - #
source - versions



A     A     A     A     A

© 2013-2017 JotHut - Online notebook

current date: Dec 23, 2024 - 12:29 p.m. EST