Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm working on a web application using Angularjs and Requirejs on the client, with Nodejs on the server.</p> <p>Here is some sample code to show you how I set it up.</p> <blockquote> <p>Note this example is showing a hash url but you can easily change that by modifying the middleware function and angular configuration</p> </blockquote> <h3>Middleware function</h3> <pre><code>isXHR: function (req, res, next) { if (req.xhr || req.get("angular-request") === "ajaxRequest") { next(); } else { var url = req.url; var urls = url.split("/"); var last = _.last(urls); urls = _.without(urls, last); url = urls.join("/") + "#/" + last //remove the hash if you want to make it html5mode; res.redirect(url); } } </code></pre> <h3>Server route configuration</h3> <pre><code>//I'm using express-namespace to group my routes app.namespace("/requirements", function(){ //Shared local variable used across the application var info = { app: { title: "Requirements", module: "app/requirements" // where the angular application stored } } //this is the main url that will user request Route.get("/", function (req, res) { res.cookie("profileRegisterationSteps", 0); res.render("app/requirements/index", info); }); //this is the url angular js will request Route.get("type", filters.isXHR, function (req, res) { res.render("app/requirements/profile/type", info); }); }) </code></pre> <h3>Client route configuration</h3> <pre><code>require(['App', 'underscore', 'ngAmd'/*angular amd*/, 'autoload'/*No Sense*/, 'appLoader' /*i used to load my scripts file for the route user requested (not all scripts files only who requested) before template laoded*/, 'appRoute'/*this is a list of routes*/], function (app, _, amd, autoload, loader, routes) { app.config(function ($routeProvider, $locationProvider, $httpProvider) { //remove a clearn URL $locationProvider.html5Mode(false); //ku dar header kan si uu server ka u ogaado Request in yahay Ajax $httpProvider.defaults.headers.common['angular-request'] = "ajaxRequest"; //Route Config var Route = $routeProvider; //get all routes objects that defined in routes module _.each(routes, function (route) { // extend the routes module objects and use its properties Route.when(route.url, _.extend(route, { //call returning function in loader module and write the promise resolve: _.extend(loader(route)) })); }); Route.otherwise({ redirectTo: "/" }); }); //start the application amd.bootstrap(app); }); </code></pre> <h3>Apploader file</h3> <pre><code>require.config({ paths: { //pages type: "Apps/requirements/pages/type" } }); define(['App'], function(app) { return function (options) { return { loader: function ($q, $rootScope) { var defer = $q.defer(); var module = options.name// the name of the route (this name corresponds to the requirejs module name above; if (!!(module)) { if (require.defined(module)) { defer.resolve(); } else { require([module], function () { $rootScope.safeApply(function () { defer.resolve(); }) }); } } else { defer.resolve(); } return defer.promise; } } } }); </code></pre> <h3>Routes file</h3> <pre><code>define(function(){ return { { name : "type", url : "/", templateUrl : "/requirements/type", view : 'services' } } }) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload