webix-hub / webix-remote-js

Simple RPC for NodeJS and Webix UI
http://webix.com
5 stars 3 forks source link

Express example #5

Open agershun opened 8 years ago

agershun commented 8 years ago

Hi

Could you add information into the documentation how to use webix-remote with Express. I spent two hours to realize that it requires installed '''body-parser''' package.

Below is the working sample:

Install prerequisites:

> npm install express
> npm install body-parser
> npm install webix-remote

Create public directory and save Webix package into public/lib/webix directory.

Create server.js file:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }))

var remote = require("webix-remote");
var api = remote.server();

app.use("/api", api.express());
app.use(express.static('public'));

api.setMethod("add", function(a,b){
   return a+b;
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

And client pagepublic/index.html:

<script src='/lib/webix/webix.js'></script>
<script src='/api'></script>
<script>
webix.remote.add(1,2).then(function(result){
    alert(result);
});
</script>

Run the server:

> node server

Open browser:

localhost:3000
gabrieltbarcelos commented 4 years ago

Thank you!! It helped a lot!