From dbd3caba1e26769fe13ec03cb9dbf2311ba60c4a Mon Sep 17 00:00:00 2001 From: "Timothy M. Preble" Date: Thu, 24 May 2018 16:22:31 -0400 Subject: [PATCH] Inital commit from RPi3 --- .gitignore | 67 ++++++++++++++++++++++++++++++++++++++++++++ README.md | 0 config.js.dist | 19 +++++++++++++ dockerfile | 14 ++++++++++ main.js.backup | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 16 +++++++++++ server.js | 65 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 257 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 config.js.dist create mode 100644 dockerfile create mode 100644 main.js.backup create mode 100755 package.json create mode 100755 server.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2352227 --- /dev/null +++ b/.gitignore @@ -0,0 +1,67 @@ +#Visual Studio Code Settings +.vscode/ + +#Ignore Real Config Files a distributed one is included for structure.s +config.js + +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + +# next.js build output +.next \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/config.js.dist b/config.js.dist new file mode 100755 index 0000000..82c124f --- /dev/null +++ b/config.js.dist @@ -0,0 +1,19 @@ +'use strict' + +var mysql = require('mysql'); + +module.exports = { + name: 'TS-api3', + hostname : 'http://localhost', + version: '3.0.1', + env: process.env.NODE_ENV || 'development', + port: process.env.PORT || 3000, + db: { + get : mysql.createConnection({ + host : 'ip or host name here', + user : 'yOuR_UsEr', + password : 'S0mePa$$w0rd', + database : 'YourDBs' + }) + } +} \ No newline at end of file diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..4891146 --- /dev/null +++ b/dockerfile @@ -0,0 +1,14 @@ +FROM timpreble/alpinearm:3.5.3 + +#Create App Directory +WORKDIR /usr/src/api + +#Install Dependencies +COPY package.json . +RUN npm install + +#Bundle ap source +COPY . . + +EXPOSE 3001 +CMD ["npm", "start"] \ No newline at end of file diff --git a/main.js.backup b/main.js.backup new file mode 100644 index 0000000..9749a52 --- /dev/null +++ b/main.js.backup @@ -0,0 +1,76 @@ +/** + * Module Dependencies + */ +const config = require('./config'), + restify = require('restify'), + mysql = require('mysql') + + +/** + * Initialize Server + */ +const server = restify.createServer({ + name : config.name, + version : config.version, + url : config.hostname +}); + +var connection = config.db.get; +server.use(restify.plugins.acceptParser(server.acceptable)); +server.use(restify.plugins.queryParser()); +server.use(restify.plugins.bodyParser()); + +/*server.get('/echo/:name', function (req, res, next) { + res.send(req.params); + return next(); +});*/ + +//rest api to get all results +server.get('/employees', function (req, res) { + //connection.query('select * from tracking limit 10', function (error, results, fields) { + connection.query('CALL CP_RPT_TodaysCounts();', function (error, results, fields) { + if (error) throw error; + res.end(JSON.stringify(results)); + }); +}); + +//rest api to get a single employee data +server.get('/employees/:id', function (req, res) { + connection.query('select * from employee where id=?', [req.params.id], function (error, results, fields) { + if (error) throw error; + res.end(JSON.stringify(results)); + }); +}); + +//rest api to create a new record into mysql database +server.post('/employees', function (req, res) { + var postData = req.body; + connection.query('INSERT INTO employee SET ?', postData, function (error, results, fields) { + if (error) throw error; + res.end(JSON.stringify(results)); + }); +}); + +//rest api to update record into mysql database +server.put('/employees', function (req, res) { + connection.query('UPDATE `employee` SET `employee_name`=?,`employee_salary`=?,`employee_age`=? where `id`=?', [req.body.employee_name,req.body.employee_salary, req.body.employee_age, req.body.id], function (error, results, fields) { + if (error) throw error; + res.end(JSON.stringify(results)); + }); +}); + +//rest api to delete record from mysql database +/*server.delete('/employees/:id', function (req, res) { + connection.query('DELETE FROM `employee` WHERE `id`=?', [req.params.id], function (error, results, fields) { + if (error) throw error; + res.end('Record has been deleted!'); + }); +});*/ + +server.get('/', function(req, res){ + console.log('Welcome Nodejs restify'); +}); + +server.listen(3001, function () { + console.log('%s listening at %s', server.name, server.url); +}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100755 index 0000000..3fb3e5a --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "nodejs-restapi-example", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "mysql": "^2.14.1", + "restify": "^5.2.0" + } +} diff --git a/server.js b/server.js new file mode 100755 index 0000000..99b7e0a --- /dev/null +++ b/server.js @@ -0,0 +1,65 @@ +/** + * Module Dependencies + */ +const config = require('./config'), + restify = require('restify'), + mysql = require('mysql') + + +/** + * Initialize Server + */ +const server = restify.createServer({ + name : config.name, + version : config.version, + url : config.hostname +}); + +var connection = config.db.get; +server.use(restify.plugins.acceptParser(server.acceptable)); +server.use(restify.plugins.queryParser()); +server.use(restify.plugins.bodyParser()); + +/*server.get('/echo/:name', function (req, res, next) { + res.send(req.params); + return next(); +});*/ + +//rest api to get all results +server.get('/api/today', function (req, res) { + //connection.query('select * from tracking limit 10', function (error, results, fields) { + connection.query('CALL CP_RPT_TodaysCounts();', function (error, results, fields) { + if (error) throw error; + res.end(JSON.stringify(results)); + //console.log(results); + //res.end(); + }); +}); + +server.get('/api/campaign',function(){ + connection.query('CALL CP_RPT_TweetsByScreenName ();',function(error, results, fields){ + if(error) throw error; + //https://www.sitepoint.com/using-node-mysql-javascript-client/ + res.end(results); + }); +}); + +/* +server.get('',function(){ + connection.query('',function(){}); +}); +*/ + +//rest api to get a single employee data +//rest api to create a new record into mysql database +//rest api to update record into mysql database +//rest api to delete record from mysql database + + +server.get('/', function(req, res){ + console.log('Welcome Nodejs restify'); +}); + +server.listen(3001, function () { + console.log('%s listening at %s', server.name, server.url); +}); \ No newline at end of file