我正在使用AngularJS和Socket.io的項目。我發現this非常好的整合例子。如何構建Node/Angular/Socket.io項目?
這是項目結構:
app.js --> app config
bower.json --> for bower
package.json --> for npm
public/ --> all of the files to be used in on the client side
css/ --> css files
app.css --> default stylesheet
img/ --> image files
js/ --> javascript files
app.js --> declare top-level app module
controllers.js --> application controllers
directives.js --> custom angular directives
filters.js --> custom angular filters
services.js --> custom angular services
bower_components/
angular/ --> angular.js
angular-socket-io/ --> socket.io adapter for angular
routes/
index.js --> route for serving HTML pages and partials
socket.js --> serve content over a socket
api.js --> serve JSON to our AngularJS client
views/
index.jade --> main page for app
layout.jade --> doctype, title, head boilerplate
partials/ --> angular view partials (partial jade templates)
partial1.jade
partial2.jade
在app.js
:現在
var express = require('express'),
routes = require('./routes'),
api = require('./routes/api'),
socket = require('./routes/socket');
...
// serve index and view partials
app.get('/', routes.index);
app.get('/partials/:name', routes.partials);
// JSON API
app.get('/api/name', api.name);
// redirect all others to the index (HTML5 history)
app.get('*', routes.index);
// Socket.io Communication
io.sockets.on('connection', require('./routes/socket'));
,而通常我只想把服務器邏輯app.js
看來,這裏的邏輯劃分在api.js
,socket.js
和index.js
之間 - 我非常喜歡這個。
但是,讓我們說,在socket.js
我需要使用api.js
定義的東西..我要補充的socket.js
一個var api = require('./api');
?
你看過Yeoman的generator-angular了嗎?這將幫助您構建您的應用程序。 –
是的,客戶端沒有問題。角度是相當酷:) 問題是服務器端..我結束了創建模塊/對象,導入一切到'app.js'並通過引用傳遞給對方(當需要).. :) – fusio