Pracuję nad projektem wykorzystującym AngularJS i Socket.io. Znalazłem this bardzo ładny przykład integracji.Jak skonstruować projekt Node/Angular/Socket.io?
Jest to struktura projektu:
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
w 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'));
Teraz, podczas gdy normalnie bym po prostu umieścić logikę serwera w app.js
wydaje się, że tu logika jest podzielona między api.js
, socket.js
i index.js
- Lubię to.
Jednak powiedzmy, że w socket.js
muszę używać coś zdefiniowanego w api.js
.. należy dodać var api = require('./api');
w socket.js
?
Czy spojrzałeś na generator kątowy dla Yeomana? To pomoże Ci uporządkować swoją aplikację. –
Tak, strona klienta nie stanowi problemu. kątowa jest całkiem fajna :) Problem był po stronie serwera. W końcu utworzyłem moduły/obiekty, zaimportowałem wszystko do pliku 'app.js' i przekazałem je przez odniesienie do siebie (w razie potrzeby) .. :) – fusio