Skip to content

Instantly share code, notes, and snippets.

@ekryski
Created March 4, 2016 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekryski/79e6ffd266eac2f8c2ad to your computer and use it in GitHub Desktop.
Save ekryski/79e6ffd266eac2f8c2ad to your computer and use it in GitHub Desktop.
Basic Feathers App
const host = 'http://localhost:3030';
let socket = io(host, {
transport: ['websockets']
});
// Set up Feathers client side
let app = feathers();
// Register hooks module
app.configure(feathers.hooks());
// Register socket.io
app.configure(feathers.socketio(socket));
// Set up a store to cache your auth token and user
app.use('storage', localstorage({ storage: window.localStorage }));
// Set up authentication
app.configure(feathers.authentication());
// Wait for socket connection
app.io.on('connect', function(){
// Authenticating using a token instead
app.authenticate({
type: 'local',
'email': 'hello@feathersjs.com',
'password': 'oh hai'
}).then(function(result){
console.log('Authenticated!', result);
// Find our users on the server via sockets
app.service('users').find().then(function(users){
console.log('Users!', users);
});
}).catch(function(error){
console.error('Error authenticating!', error);
});
});
import AsyncStorage from 'react-native';
import feathers from 'feathers/client'
import hooks from 'feathers-hooks';
import socketio from 'feathers-socketio/client'
import authentication from 'feathers-authentication/client';
import localstorage from 'feathers-localstorage';
// This is required for socket.io-client
if (window.navigator && Object.keys(window.navigator).length == 0) {
window = Object.assign(window, {navigator: {userAgent: 'ReactNative'}});
}
const host = 'http://localhost:3030';
let socket = io(host, {
transport: ['websockets']
});
// Set up Feathers client side
let app = feathers();
// Register hooks module
app.configure(hooks());
// Register socket.io
app.configure(socketio(socket));
// Set up a store to cache your auth token and user
app.use('storage', localstorage({ storage: AsyncStorage }));
// Set up authentication
app.configure(feathers.authentication());
// Wait for socket connection
app.io.on('connect', function(){
// Authenticating using a token instead
app.authenticate({
type: 'local',
'email': 'hello@feathersjs.com',
'password': 'oh hai'
}).then(function(result){
console.log('Authenticated!', result);
// Find our users on the server via sockets
app.service('users').find().then(function(users){
console.log('Users!', users);
});
}).catch(function(error){
console.error('Error authenticating!', error);
});
});
// app.js
let feathers = require('feathers');
let rest = require('feathers-rest');
let socketio = require('feathers-socketio');
let hooks = require('feathers-hooks');
let memory = require('feathers-memory');
let authentication = require('feathers-authentication');
let bodyParser = require('body-parser');
let handler = require('feathers-errors/handler');
// A Feathers app is the same as an Express app
let app = feathers();
// Parse HTTP JSON bodies
app.use(bodyParser.json());
// Parse URL-encoded params
app.use(bodyParser.urlencoded({ extended: true }));
// Register hooks module
app.configure(hooks());
// Add REST API support
app.configure(rest());
// Configure Socket.io real-time APIs
app.configure(socketio());
// Register our authentication plugin
app.configure(authentication({ idField: 'id' }));
// Register our memory "users" service
app.use('/users', memory());
// Register a nicer error handler than the default Express one
app.use(handler());
// Register a before hook to hash passwords
app.service('users').before({
create: authentication.hooks.hashPassword()
});
// Start the server
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment