patternjavascriptMinor
How well or poorly structured are my routes in this NodeJS app?
Viewed 0 times
thisroutesapparepoorlynodejsstructuredwellhow
Problem
This is for an app built with Express. One of my concerns is the routes ending with
Here is what I got for routes
app.js
```
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon(path.join(__dirname, 'public/images/template/favicon.ico')));
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', function(req, res){
res.render('home', {
title: 'Home'
});
});
app.get('/features-bruxzir-zirconia-dental-crown/', function(req, res){
res.render('features', {
title: 'BruxZir Features'
});
});
app.get('/science-bruxzir-zirconia-dental-crown/', function(req, res){
res.render('science', {
title: 'Scientific Validation'
});
});
app.get('/video-bruxzir-zirconia-dental-crown/', function(req, res){
res.render('videos', {
title: 'BruxZir Video Gallery'
});
});
app.get('/cases-bruxzir-zirconia-dental-crown/', function(req, res){
res.render('cases', {
title: 'Before & After Case Gallery'
});
});
app.get('/testimonials-bruxzir-zirconia-dental-crown/', function(req, res){
res.render('testimonials', {
title: 'Bruxzir Testimonials'
});
});
app.get('/authoriz
/ which I did because our previous site was an ASP app. So they named the folders that way for SEO purposes, then the page would be index.aspx .But in my case I just did, for example science.jade and routed it from /science-bruxzir-zirconia-dental-crown/Here is what I got for routes
app.js
```
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon(path.join(__dirname, 'public/images/template/favicon.ico')));
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', function(req, res){
res.render('home', {
title: 'Home'
});
});
app.get('/features-bruxzir-zirconia-dental-crown/', function(req, res){
res.render('features', {
title: 'BruxZir Features'
});
});
app.get('/science-bruxzir-zirconia-dental-crown/', function(req, res){
res.render('science', {
title: 'Scientific Validation'
});
});
app.get('/video-bruxzir-zirconia-dental-crown/', function(req, res){
res.render('videos', {
title: 'BruxZir Video Gallery'
});
});
app.get('/cases-bruxzir-zirconia-dental-crown/', function(req, res){
res.render('cases', {
title: 'Before & After Case Gallery'
});
});
app.get('/testimonials-bruxzir-zirconia-dental-crown/', function(req, res){
res.render('testimonials', {
title: 'Bruxzir Testimonials'
});
});
app.get('/authoriz
Solution
From a once over of the code and the comments:
- This code is fine, especially if you have only a dozen routes, I would not advise you to build your own route building infrastructure for such a small amount of routes
- Dont worry about the
/
- Consider having your routes in separate files as per @GnrlBzik
Context
StackExchange Code Review Q#38056, answer score: 2
Revisions (0)
No revisions yet.