0.0.7 • Published 8 years ago
trangar-react-utils v0.0.7
React Utils
Utility class made of regularly used components in Trangar's projects
Install via NPM
npm install trangar-react-utils --save
File structure
server.js
var server = require('trangar-react-utils/server');
server.start({
react: {
file: __dirname + '/src/client.jsx',
outputFile: __dirname + '/public/build.js'
},
server: {
port: 8181,
publicDirectory: __dirname + '/public/',
defaultFile: __dirname + '/public/index.html'
},
actions: {
directory: __dirname + '/actions/',
outputFile: __dirname + '/public/server.js'
},
database: {
type: 'mongodb',
url: '<mongo url goes here>',
directory: __dirname + '/database/models/'
}
});
Note: Only mongodb is supported for now
actions/user/index.js
// in your browser, call "server.user.loadByID(id)"
function loadByID(id){
return this.user.findOne({ _id: id })
}
// in your browser, call "server.user.login(username, password)"
function login(username, password){
return this.user.findOne({ name: username })
.then(validatePassword);
}
module.exports = {
get: [loadByID],
post: [login]
};
schema/user.js
module.exports = function(mongoose){
// schema definition
var user = mongoose.Schema({
name: String,
password: {
hash: String,
salt: String,
iterations: Number
}
});
// TODO: Add methods here
// see http://mongoosejs.com/docs/index.html for more info
// return the model
return mongoose.model("User", user);
};
src/clients.jsx
var React = require('react'),
ReactDom = require('react-dom');
class Main extends React.Component {
render(){
return <h1>Hello from main</h1>
}
}
// here you have access to server.user.login(username, password).then(...) and server.user.loadByID(id).then(...)
var div = document.createElement('div');
ReactDom.render(<Main />, div);
document.body.appendChild(div);
Run
node server