feathersjs / docs

[MOVED] Legacy Feathers documentation
https://crow.docs.feathersjs.com/
MIT License
242 stars 532 forks source link

Cookbook Suggestion: the most simple feathers example #1577

Closed edwardsmarkf closed 1 year ago

edwardsmarkf commented 1 year ago

I believe it would be very useful to have a very simple example of a working feathers server. This extraordinary simple example assumes you are starting out with absolutely nothing except for a server connected to the internet, and a chrome browser.

It also assumes you are eager to see the greatest framework ever created working in a matter of seconds.

This example will simply return the server date.

WARNING - once you see this working, you will wonder why you wasted so much of your life looking at rails, trails, snails, meteor, deepstream, etc.

1) install npm and feathers:

  sudo dnf  -y  install  node   npm ;
  sudo npm install @feathersjs/feathers --save  ;
  sudo npm install --global  @feathersjs/cli ;

2) feathers generate app ;

3) feathers generate service ; -- select "A custom service" -- suggested name of your service is "tester"

4) edit this file: ./src/services/tester/tester.class.js

-- change:

  async find (params) {
    return [];
  }

-- to:

  async find (params) {
    return new Date();      // return [];
  }

5) start your server!

firewall-cmd --add-port=3030/tcp;  ## or systemctl stop firewalld.service ;
DEBUG=*   npm start ;   ###  DEBUG is optional for now 

6) open a browser tab, enterabout"blank to make sure tab is really blank!

7) enter the console and add the following lines

/* load in libraries */
[ 'https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js'
, 'https://unpkg.com/@feathersjs/client@^3.0.0/dist/feathers.js'
].forEach( (src) => {
    let script = document.createElement('script'); 
            // script.setAttribute('crossOrigin',  'anonymous' );  /* optional  */
    script.src =  src;
            // script.type = 'javascript';   /*optional  */
    script.src = src; 
    script.async = false;
    document.head.appendChild(script);
});

8) enter the following lines in the console

/* initialize constants, PAUSE here for a moment for previous scripts to load  !! */

const app = feathers();
app.configure(feathers.socketio(io('ws://###.###.###.###:3030')));  /* put in your server IP ## */

/* one line of code (shown three for readability) to immediately execute promise */

(async () => { 
  try { let result = await app.service('tester').find() ; 
    console.log('the result is: ' + JSON.stringify(result)) }
  catch (e) { console.log(e); }  } 
)() ;

You should now see your server date/time in your browser console.

edwardsmarkf commented 1 year ago

i understand this is covered in https://feathersjs.com/guides/basics/starting.html -- although i was trying to demonstrate feathers using a mere one line of new code with the CLI.