gpt4 book ai didi

javascript - 对象 # 没有方法 'use' [Express+ShareJS]

转载 作者:行者123 更新时间:2023-11-28 20:28:18 25 4
gpt4 key购买 nike

环境

  • Express 3.2.4
  • ShareJS 0.6.2
  • OS X 10.8.3
<小时/>

目标

我正在尝试让 ShareJS 在 Express 应用程序中运行。

<小时/>

问题

我在运行 node app.js 时收到以下错误:

/Users/eric/Documents/Projects/collabit/node_modules/share/src/server/index.coffee:52
server.use(options.staticpath, connect["static"]("" + __dirname + "/../.
^
TypeError: Object #<Server> has no method 'use'
at Function.create.attach.attach (/Users/eric/Documents/Projects/collabit/node_modules/share/src/server/index.coffee:52:14)
at Object.<anonymous> (/Users/eric/Documents/Projects/collabit/app.js:43:16)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)

我使用express cli 界面生成了一个应用程序,并且在决定换入 ShareJS 之前,使用 SocketIO 工作得很好。我很确定我的问题是如何构建服务器,但我不太清楚我应该如何创建服务器对象以保留 Express 功能但允许 ShareJS 附加.b

<小时/>

代码

/**
* Module dependencies.
*/

var express = require('express')
, sharejs = require('share')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');

var app = express();

app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
app.use(express.errorHandler());
});

app.get('/', routes.index);
app.get('/users', user.list);

var server = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});

/**
* ShareJS Code
*/

var options = {db: {type: 'none'}};
sharejs.server.attach(server, options);
<小时/>

问题

创建服务器对象的正确方法是什么,以便允许 ShareJS 附加到它,同时保留 Express 功能。

<小时/>

谢谢!

提前感谢您的帮助,如果需要任何其他信息,请告诉我。

最佳答案

我也有类似的问题。经过大量的试验和错误,这对我有用:

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

var app = express();
var server = http.createServer(app);

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(app, options);

app.use(app.router);

app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);

server.listen(8000, function () {
console.log('Server running at http://127.0.0.1:8000/');
});

app.get('/', function(req, res) {
res.render('share.html');
});

然后在views/share.html中我有标准的示例html:

<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script src="http://ajaxorg.github.com/ace/build/src/ace.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="/channel/bcsocket.js"></script>
<script src="/share/share.js"></script>
<script src="/share/ace.js"></script>
<script>
$(document).ready(function() {
var editor = ace.edit("editor");

sharejs.open('hello', 'text', 'http://localhost:8000/channel', function (error, doc) {
doc.attach_ace(editor);
});
});
</script>
<style>
#editor {
width: 200px;
height: 100px;
}
</style>
</head>
<body>
<div id="editor"></div>
</body>
</html>

关于javascript - 对象 #<Server> 没有方法 'use' [Express+ShareJS],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16931329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com