Share.js not working with Express.js

310 views Asked by At

Server Side code,

var express = require('express'); //Web Framework
var app = express();
var http = require('http').Server(app); //HTTP server module
var connect = require('connect'),
    sharejs = require('share').server;

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});


var options = {db: {type: 'none'}}; // See docs for options. {type: 'redis'} to enable persistance.

// Attach the sharejs REST and Socket.io interfaces to the server
sharejs.attach(server, options);

app.get('/', function (req, res) {
  res.sendFile('index.html', { root: __dirname });
})


http.listen(3000, function () {
    console.log('listening on *:3000');
});

But I cant get the share.js to work.

It gives an error, ReferenceError: server is not defined

How do I get express to work along with share.js?

1

There are 1 answers

3
James Donnelly On

ShareJS 0.6

If you're using ShareJS 0.6, the issue is that you're using require('share').http instead of require('share').server.

Change:

var connect = require('connect'),
    sharejs = require('share').http;

To:

var connect = require('connect'),
    sharejs = require('share').server;

ShareJS 0.7

If you're using ShareJS 0.7, the attach method has been deprecated. See this answer for a workaround.


Update

Now that your answer has been updated, the problem is that you haven't defined the server variable. The documentation states that you can define this using:

var server = connect(
      connect.logger(),
      connect.static(__dirname + '/public')
    );