m Feathers - CRUD api framework
Key Points
- Feathers can generate CRUD api frameworks based on simple domain models
- CRUD services are automatically used in the framework via http calls
- all services can have before and after hooks to customize the request and response
- Feathers has MANY available plugins for any app scenario
- consider creating a VUE app generator for the models in the feathers app
Quick overview of Feathers - 2023
Key Issues
- Lacks the view join capabilities of Grails
- Lacks the ability to generate SCRUD front-end like Grails ( need to use Marmelab EOS )
- If you define separate apps based on role issues exist
- can't easily do a single sign-on for a user across multiple Feathers apps without SSO, Openid Connect
or create 1 app with multiple roles and a single token with appropriate features enabled - to use same services in multiple apps must redefine the services in each app ( need a redirect to a 2nd app to run the service, return the results )
- can't easily do a single sign-on for a user across multiple Feathers apps without SSO, Openid Connect
- more
References
Key Concepts
MANY Nodejs basic tips and tutorials
https://www.freecodecamp.org/news/search/?query=nodejs
https://flaviocopes.com/node-read-csv/
nodejs-servers-How to Start a Node Server Examples with the Most Popular Frameworks.pdf
nodejs-the-complete-guide-course-at-udemy-.pdf
https://www.freecodecamp.org/news/how-to-build-a-todo-app-with-react-typescript-nodejs-and-mongodb/
https://www.freecodecamp.org/news/create-a-professional-node-express/
https://www.freecodecamp.org/news/node-js-production-checklist/
https://www.freecodecamp.org/news/stripe-and-node-js-4-best-practices-and-examples/
Node.js debug
https://www.freecodecamp.org/news/watch-sockets-with-this-grunt-plugin/
https://www.freecodecamp.org/news/node-js-debugging/
Node AWS
https://www.freecodecamp.org/news/how-to-reuse-packages-with-aws-lambda-functions-using-amplify/
Feathers Concepts
Feathers API Services
Feathers Event Support
https://feathersjs.com/api/events.html
Events are the key part of Feathers real-time functionality. All events in Feathers are provided through the NodeJS EventEmitter interface. This section describes
- A quick overview of the NodeJS EventEmitter interface
- The standard service events
- How to allow sending custom events from the server to the client
Feathers REST API support
Feathers uses the concept of a Model object for an entity to map to the configured data source.
IF the Model and the data source match on field names for that Entity then the basic CRUD functions of create, update, patch will work without additional code in the API for that entity
IF you need added changes to the data model ( eg calculate fundsAvailable = fundsBalance - fundsAllocatedOrders ) then you need a before hook method to do those added data changes before the write to the data source
Example of a patch method that can get and manage account data before patch executes
Feathers Client Authentication ( vs Axios )
Feathers Web Sockets using socket.io for Trade Desk
example is
- when you accept my offer to sell energy
- then my energyBalance and energyAllocatedOffers drop
- so I can see the change dynamically on my trade desk
Node configuration module to manage config files directory using environment variable NODE_ENV
https://zetcode.com/javascript/nodeconfig/
Node-config tutorial shows how to create configuration files for Node applications with node-config module.
Node-config
Node-config creates configuration files for application deployments.
Node-config allows us to define a set of default parameters and extend them for different deployment environments (development, qa, staging, production, etc.).
The configuration files are located in the default config
directory. The location can be overriden with the NODE_CONFIG_DIR
environment variable. The NODE_ENV
environment variable contains the name of our application's deployment environment; it is development
by default.
Node config supports various configuration file formats, including JSON, YAML, properties, or XML. The default configuration file is default.json
(or default.yaml
, default.xml
). If we use a production deployment, then the configuration is loaded from production.json
.
Setting up Node-config
First, we install node-config
.
$ node -v v11.5.0
We use Node version 11.5.0.
$ npm init -y
We initiate a new Node application.
$ npm i config
We install node-config with nmp i config
.
$ npm i js-yaml
In addition, we install js-yaml
for YAML support.
Node-config example
The following example retrieves configuration data with config
package.
{ "app": { "port": 3000 }, "db": { "host": "localhost", "port": 27017, "name": "ydb" } }
We have default.json
in the config
directory.
const config = require('config'); let appPort = config.get('app.port'); console.log(`Application port: ${appPort}`); let dbHost = config.get('db.host'); console.log(`Database host: ${dbHost}`); let dbPort = config.get('db.port'); console.log(`Database port: ${dbPort}`); let dbName = config.get('db.name'); console.log(`Database name: ${dbName}`); console.log('NODE_ENV: ' + config.util.getEnv('NODE_ENV'));
We load the config
package and get the values with config.get()
function. The default deployment type is specified in NODE_ENV
.
$ node simple.js Application port: 3000 Database host: localhost Database port: 27017 Database name: ydb NODE_ENV: development
This is the output.
Node-config example II
We change the configuration file to YAML and set a production deployment environment.
app: port: 3000 db: host: localhost port: 27017 name: ydb
This is default.yaml
file.
app: port: 3300 db: host: localhost port: 27017 name: mydb
This is production.yaml
file.
const config = require('config'); let appPort = config.get('app.port'); console.log(`Application port: ${appPort}`); ...
The simple.js
file is the same.
$ node simple.js Application port: 3000 Database host: localhost Database port: 27017 Database name: ydb NODE_ENV: development
This is the output for the default environment. The configuration is loaded from default.yaml
.
$ set NODE_ENV=production $ node simple.js Application port: 3300 Database host: localhost Database port: 27017 Database name: mydb NODE_ENV: production
We change the NODE_ENV
variable with the set
command. (Use export
on Linux.) Now the configuration data is loaded from production.yaml
file.
In this tutorial, we have used node-config
package to create configuration files for our Node.js application.
Feathers configuration module for environments
https://bailer.gitbooks.io/feathersjs/content/configuration/readme.html
feathers-configuration
allows you to load default and environment specific JSON and/or JS configuration files and environment variables and set them on your application. In a generated application the config/default.json
and config/production.json
files are set up with feathers-configuration automatically.
Here is what it does:
- Given a
NODE_CONFIG_DIR
environment variable it will load adefault.json
in that path, the default here is./config
. - When the
NODE_ENV
is notdevelopment
, also try to load<NODE_ENV>.json
in that path and merge both configurations (with<NODE_ENV>.json
taking precedence) - Go through each configuration value and sets it on the application (via
app.set(name, value)
).- If the value is a valid environment variable (e.v.
NODE_ENV
), use its value instead - If the value start with
./
or../
turn it to an absolute path relative to the configuration file path - If the value starts with a
\
, do none of the above two
- If the value is a valid environment variable (e.v.
Feathers Examples
Query with custom limit set for returned items in list
{{base_url}}/m-countries/?show=yes&$limit=100
Feathers modules
Feathers Development Model
use VSCode
create the api service
setup configs for environments
run the api service for an environment
Nodejs Basics
https://www.w3schools.com/nodejs/ref_modules.asp
Node.js CLI app with OAuth security
https://developer.okta.com/blog/2019/06/18/command-line-app-with-nodejs
node-cli-ex-oauth-developer.okta-Build a Command Line Application with Nodejs.pdf
Modules - builtin and custom
https://www.w3schools.com/nodejs/nodejs_modules.asp
Create a module that returns the current date and time:
exports.myDateTime = function () {
return Date();
};
Use the exports
keyword to make properties and methods available outside the module file.
Save the code above in a file called "myfirstmodule.js
Include modules with require
var http = require('http');
var dt = require('./myfirstmodule');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write("The date and time are currently: " + dt.myDateTime());
res.end();
}).listen(8080);
Http module to create a server
Need a header record for type
Pass req, res variables to set request, get response
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(req.url);
res.end();
}).listen(8080);
NPM package manager installed with Node.js
https://www.w3schools.com/nodejs/nodejs_npm.asp
NPM is a package manager for Node.js packages, or modules if you like.
www.npmjs.com hosts thousands of free packages to download and use.
The NPM program is installed on your computer when you install Node.js
A package in Node.js contains all the files you need for a module.
Modules are JavaScript libraries you can include in your project.
npm install mypackage
Events module
https://www.w3schools.com/nodejs/nodejs_events.asp
you can create-, fire-, and listen for- your own events.
Similar to Java beans where events are built-in with create event, addListener, fireEvent
Objects in Node.js can fire events, like the readStream object fires events when opening and closing a file:
Create eventEmitter
var events = require('events');
var eventEmitter = new events.EventEmitter();
Email module
https://www.w3schools.com/nodejs/nodejs_email.asp
Potential Value Opportunities
Potential Challenges
Candidate Solutions
Step-by-step guide for Example
sample code block