未加星标

Using FeathersJS To Access MariaDB With Minimal Coding Part III

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

Simple CRUD Example Using FeathersJS And Sequelize, Part III


Using FeathersJS To Access MariaDB With Minimal Coding Part III

This is the final article of a three-part series. This series of articles will focus on building a complete CRUD interface. The reader is encouraged to optionally install Postman and simulate the same steps using a REST interface.

SECURITY INTRODUCTION

So far we have been doing simple cut/paste right into our FeathersDB page. In this article, we will cover how to put the same code into ANY webpage. And for this article, you dont even need a webpage! You can open up a new tab, no webpage or website is required.

Once upon a time, we could have done this into any page without any security issues to be concerned about. However, as the UK’s “The Telegraph” stated several years ago: ‘The internet can be a dark and dangerous place’ . So, we must secure our calls via SSL , or secure socket layer . For an excellent overview on how SSL works, please refer to this article . Recently, most major browsers have started to seriously enforce this requirement.

So let’s secure our FeathersJS server. This is actually quite easy and therefore this will be the shortest article (yes aren’t you relieved?).


Using FeathersJS To Access MariaDB With Minimal Coding Part III
PREREQUISITES
Using FeathersJS To Access MariaDB With Minimal Coding Part III

This section will require a domain name if you don’t already have one. I prefer NameCheap since as the name implies, a domain name can be purchased for as little as $1.

We will also be required to obtain an ssl certification . I will be using LetsEncrypt since it is free and we dont require a high-grade certification for our purposes.

INSTALL LETSENCRYPT

This of course will depend largely on your server type. For Centos7, these are the required steps:

sudo yum --assumeyes install yum-utils; sudo yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional ; sudo yum --assumeyes install python2-certbot-apache; CREATE A CERTIFICATION
Using FeathersJS To Access MariaDB With Minimal Coding Part III

All that is required to create your certification is to enter the following command:

sudo certbot certonly ; ## the certonly switch will not alter your apache configuration.

You will be prompted with a series of questions. Notice that I choose option two, “Spin up a temporary webserver (standalone)” , but you may choose any option. Pay particular attention to the question regarding your domain name. It is recommended that you certify both the domain name and with the www prefix.


Using FeathersJS To Access MariaDB With Minimal Coding Part III

You may also be required to enter:

sudo chmod 755 /etc/letsencrypt/archive ; sudo chmod 755 /etc/letsencrypt/archive/YOURDOMAINNAME ; sudo chmod 755 /etc/letsencrypt/live/ ;

depending on your installation.

I like to double-check to make sure the certification files are available to me:

ls -l /etc/letsencrypt/live/YOURDOMAINNAME ; ## do NOT use sedo!
Using FeathersJS To Access MariaDB With Minimal Coding Part III
NODE PACKAGES

Now we have to manually install the new required node packages:

npm install https --save ; npm install fs --save; CODE MODIFICATIONS

Locate your ./src/index.js script and replace the line

const server = app.listen(port);

with the following code:

/* commented out: const server = app.listen(port); */ const https = require('https'); const fs = require('fs'); const privkey_pem = app.get('privkey_pem'); // referenced in ./config/default.json const cert_pem = app.get('cert_pem'); // referenced in ./config/default.json const chain_pem = app.get('chain_pem'); // referenced in ./config/default.json const server = https.createServer({ key: fs.readFileSync(privkey_pem), cert: fs.readFileSync(cert_pem), ca: fs.readFileSync(chain_pem), }, app).listen(port); app.setup(server);

Optionally, find the bottom line:

logger.info('Feathers application started on http://%s:%d', app.get('host'), port)

and change http:// with https:// for the sake of convenience.

Next, go into your ./config/default.json script and add the following:

"privkey_pem" : "/etc/letsencrypt/live/YOURDOMAINNAME/privkey.pem", "cert_pem" : "/etc/letsencrypt/live/YOURDOMAINNAME/cert.pem", "chain_pem" : "/etc/letsencrypt/live/YOURDOMAINNAME/chain.pem",

I suggest putting these three certbot lines right below the “port”: 3030, line.

And while in this file, lets update our host line to reflect the domain name. Replace the IP number under host: with your domain name.

RESET MariaDB TABLE

After all this testing, you may need to go back into your database console and enter the following to reset your table:

delete from testConnect ; alter table testConnect set AUTO_INCREMENT=1 ; START FeathersJS APPLICATION

Finally start our application once again:

npm start;

Notice something different? Instead of giving us an IP number, FeathersJS is now giving us our domain name instead!

ADD BROWSER CODE

Finally, open a new browser window (control-T) or RIGHT-CLICK and select new tab. Notice that we are NOT using the conventional FeathersJS window, instead we are using a blank window. All steps from thefirst article should work in your new browser window, with one exception:

Remember where we used:

const socket = io() ;

Instead, lets use our domain name. Dont forget the https:// prefix!

const socket = io('https://YOURDOMAINNAME:3030') ;

Note: If you are still testing using the FeathersDB default website, this last step is unnecessary, for now.

All code is displayed again for your convenience:

[ '//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js' , '//unpkg.com/@feathersjs/<a href="/cdn-cgi/l/email-protection" data-cfemail="f99a95909c978db9">[email protected]</a>^3.0.0/dist/feathers.js' ].forEach( (src) => { let script = document.createElement('script'); script.src = src; script.async = false; document.head.appendChild(script); }); /*** Dont forget to pause for a moment here! ***/ const app = feathers(); const socket = io('https://YOURDOMAINNAME:3030') ; // this line was: const socket = io() ; app.configure(feathers.socketio(socket)); /* optionally, establish a listener for create(POST), patched(PATCH), update(PUT), and remove(DELETE) */ app.setup(); /* optional, but good practice to include anyways */ app.service('tiny-connect').on('created', createMessage => { console.log('create(POST) : ', JSON.stringify(createMessage))}); app.service('tiny-connect').on('updated', updateMessage => { console.log('update(PUT) : ', JSON.stringify(updateMessage))}); app.service('tiny-connect').on('patched', patchMessage => { console.log('patch(PATCH) : ', JSON.stringify(patchMessage ))}); app.service('tiny-connect').on('removed', removeMessage => { console.log('removed(DELETE): ', JSON.stringify(removeMessage))}); /* async functions */ async function createTiny(data) { /* create(POST) */ let createResult = await app.service('tiny-connect').create(data); console.log('createTiny function result: ' + JSON.stringify(createResult) ); return createResult; }; async function getTiny(id) { /* get(GET) (just one by index) */ let getResult = await app.service('tiny-connect').get(id); console.log('getTiny function: ' + JSON.stringify(getResult) ); return getResult; }; async function patchTiny(id, data, params) { /* patch(PATCH) */ let patchResult = await app.service('tiny-connect').patch(id, data, params); console.log('patchTiny function: ' + JSON.stringify(patchResult) ); return patchResult; }; async function findTiny(query) { /* find(GET) (get all or by query) */ console.log('Query function using: ' + JSON.stringify(query) ); let findResult = await app.service('tiny-connect').find(query); console.log('findTiny function: ' + JSON.stringify(findResult) ); return findResult; }; async function removeTiny(id, params) { /* remove(DELETE) */ let removeResult = await app.service('tiny-connect').remove(id, params); console.log('removeTiny function: ' + JSON.stringify(removeResult) ); return removeResult; }; /* create row */ let createTinyData = { name: 'mark edwards', address: '123 Swallow Lane' } ; createTiny(createTinyData).then((value) => { /* create(POST) */ console.log('createTiny: ' + JSON.stringify(value)); }); /* display row */ let findTinyResultAll; findTiny(null).then((value) => { /* find(GET) (find all) */ findTinyResultAll = value; console.log('findTiny without query: ' + JSON.stringify(findTinyResultAll) ); }); let findTinyResultOne; let findTinyData = {query:{ id: 1}}; findTiny(findTinyData).then((value) => { /* find(GET) (with data) */ findTinyResultOne = value; console.log('findTiny with ' + findTinyData + ' query: ' + JSON.stringify(findTinyResultOne) ); }); let getTinyResult = null; let getKey = 0; getTiny(getKey).then( value => { /* get(GET) */ getTinyResult = value; console.log('getTiny(' + getKey + ') : ' + JSON.stringify(getTinyResult) ); }); /* update(PATCH) row */ let patchResult = null; let patchKey = 0; let patchData = {address: '5678 There Street!' }; patchTiny(patchKey, patchData ).then( value => { /* patch(PATCH) */ patchResult = value; console.log('patchResult: ' + JSON.stringify(patchResult)); }); /* remove(DELETE) row */ let removeResult = null; let removeKey = 0; removeTiny(removeKey).then( value => { /* remove(DELETE) */ removeResult = value; console.log('removeResult(' + removeKey + ': ' + JSON.stringify(removeResult)); });

Here is what you should see in an empty browser window:


Using FeathersJS To Access MariaDB With Minimal Coding Part III
CONCLUSION

You now are able to have full CRUD capability using any Sequelize database inside of any webpage.

I hope you have enjoyed this three-part article series.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:Using FeathersJS To Access MariaDB With Minimal Coding Part III
本站链接:https://www.codesec.net/view/621372.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(46)