glo*_*log 10 javascript node.js express angularjs
使用node,express,socket.io,jade和angular.得到错误:TypeError: Cannot read property 'apply' of undefined.有什么建议?
index.js:
module.exports = function(app, res) {
res.render('index', { title: 'Express' });
var io = app.get('io');
io.on('connection', function(socket){
});
};
Run Code Online (Sandbox Code Playgroud)
index.jade:
extends layout
block content
script.
var app = angular.module('hackigur', []);
var socket = io.connect();
var refreshTimer = 10;
app.controller('UpdateController', function($scope){
//socket.on('update', function(msg){
//$scope.refreshTimer = msg;
//$scope.$apply();
//});
setInterval(secondTick,1000);
function secondTick() {
if(refreshTimer != 0) {
refreshTimer -= 1;
}
$scope.refreshTimer = refreshTimer;
$scope.$apply();
};
});
h1= title
p Welcome to #{title}
div(ng-controller="UpdateController")
p(ng-bind="refreshTimer")
Run Code Online (Sandbox Code Playgroud)
layout.jade:
doctype html
html(ng-app="hackigur")
head
title= title
script(src = "/socket.io/socket.io.js")
script(src = "/js/angular/angular.min.js")
body
block content
完整错误:
Server listening on port 3000
TypeError: Cannot read property 'apply' of undefined
at Server.(anonymous function) [as on] (D:\Projects\hackigur\node_modules\so
cket.io\lib\index.js:364:15)
at module.exports (D:\Projects\hackigur\server\api\index.js:30:8) at ...
我router该打电话给我index.js传递app的module.export是这样的:
module.export = function (app) {
app.get( ... , function(..., res) {
require(index.js)(app)(res);
};
Run Code Online (Sandbox Code Playgroud)
我需要app为我的外部声明一个变量module.export:
var x;
module.export = function (app) {
x = app;
app.get( ... , function(..., res) {
require(index.js)(x)(res);
};
Run Code Online (Sandbox Code Playgroud)
不完全理解它为什么起作用,但似乎通过应用上述内容传递了正确的app对象app.get.
| 归档时间: |
|
| 查看次数: |
51825 次 |
| 最近记录: |