I'm brand new to the MEAN stack and I am trying to figure out the best way to handle my routing, either express.Router()
or AngularJS's controllers (https://docs.angularjs.org/tutorial/step_07).
I am using Express v4.12.0, Angular v1.3.13, EJS v1 (for templating).
I really like how clean the URLs are from Express's routing (/items/itemId
) and I'm not too fond of Angular's, since it uses hashs in the URL (/items#/items/itemId
).
Right now I have a single page that lists my items (players.ejs
):
<!DOCTYPE html>
<html lang="en" ng-app='app'>
<head>
<title>Fantasy Football Helper | <%- title %></title>
<% include partials/head %>
<script type="text/javascript" src="/javascript/app/playersApp.js"></script>
</head>
<body class="container">
<% include partials/header %>
<main>
<div class="row">
<div class="col-sm-8">
<h2>Fantasy Players</h2>
<div ng-controller="PlayerController">
<div class="player" ng-repeat="player in players">
<p>
<a href="/players/{{ player._id }}"><strong>{{ player.name }} - {{ player.position }} - {{ player.team }}</strong></a>
</p>
</div>
</div>
<!-- Ideally, other CRUD UI goes here -->
</div>
<% include partials/sidebar %>
</div>
</main>
<footer>
<% include partials/footer %>
</footer>
</body>
</html>
I would like to reuse this players.ejs
template for each of my CRUD operations for these players
. Currently I am using Express's routing. I have the following route in my main app.js
:
var players = require('./routes/players');
app.use('/players', players);
And I have the following routes inside of the players
route (routes/players.js
):
var express = require('express');
var router = express.Router();
/* GET /players listing. */
router.get('/', function(req, res, next) {
res.render('players', { title: 'Players'});
});
/* GET /player details. */
router.get('/:id', function(req, res, next) {
res.render('players', { title: 'Fantasy Players'});
});
module.exports = router;
Is there a way, using Express's routing, to have a single EJS template that serves up different partials depending on the http operation?
Note: I've been referencing the following guides: