I've just deployed my first AngularJS application to Heroku using Node with the script below.
Now the URL to the site is http://project.herokuapp.com/. However, I would like it to be http://project.herokuapp.com/beta. How can I accomplish this?
var gzippo = require('gzippo');
var express = require('express');
var app = express();
app.use(gzippo.staticGzip("" + __dirname + ""));
app.listen(process.env.PORT || 5000);
With express (4x) Router() you can do that. For example:
// declare your router
var router = express.Router();
// define your routes
router.get('/', function(req, res) {
// do your stuff
});
// define other routes and so on...
// ...and make shure all your routes will be prefixed with /beta
app.use('/beta', router);
Now, http://project.herokuapp.com/beta is the main path, and other routes will be always prefixed with /beta
Related
I have the following structure
project
-app/
--controllers/
---home.js
--models/
---home.js
--views/
---home.html
-db/
--db.js
-index.js
my controller home.js looks like this:
var express = require('express');
var router = express.Router();
var database = require('../models/home');
var path = require('path');
router.get("/", function(request, response) {
response.sendFile(path.join(__dirname, '../views', 'home.html'));
});
module.exports = router;
Now I want to make that route useful, so in my index.js I have:
var express = require('express');
var app = express();
var port = 3000;
app.use(require('./app/controllers/home')); //<-- this is what I ask for
app.listen(3000, function(err ) {
//......
})
But now I have more then just route to 'home', so instead of
app.use(require('./app/controllers/home'));
app.use(require('./app/controllers/about'));
app.use(require('./app/controllers/etc'));
I read that I can use:
app.use(require('./app/controllers/'));
But I get error that the module cannot be found. Can you suggest me how can I easily get all my routes in use? Thanks :)
If I understand your question right you're trying to route a router to a different relative path.
You can set a relative path by passing that path as the first parameter. Then any route inside of your router that uses app.get/use/... will start from that relative path.
Inside index.js
app.use("/home", require('./app/controllers/home'));
app.use("/about", require('./app/controllers/about'));
app.use("/etc", require('./app/controllers/etc'));
inside ./app/controllers/home and other routes
router.get("/", ...) // this will be located at localhost/home
router.get("/myhouse, ...) // this will be located at localhost/home/myhouse
Also worth metioning you can route a router inside a router
router.use("/", require('path/to/another-router.js'))
I am fairly new to node.js.
I am currently using lowdb for my database while I get the app started.
In the index.js file I have:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var request = require('request');
var path = require('path');
var low = require('lowdb');
var db = low('db.json');
var routes = require('./routes');
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, '/public')));
app.use('/', routes);
server.listen(3000, function(){
console.log('listening on port 3000');
});
Then in my routes file I have something like:
var express = require('express');
var router = express.Router();
var account = require('./controllers/accounts.js');
router.post('/login', account.login);
router.get('/', function(req, res){
res.render('home');
});
module.exports = router;
And finally in my account controller I have:
exports.login = function(req, res){
var email = req.body.email;
var password = req.body.password;
...
}
The routing to controller system works. However I need to access the lowdb object from all of my routed controller functions (and also possibly elsewhere).
If in app.js I set:
global.db = db;
Then it seems to work, but from what I have read, setting this globally isn't the ideal solution. What is the appropriate way to be able to access the db from the controller files without having to set the db connection in every single file.
In your index.js file (application root) store the lowdb object in your express object :
var app = express();
var db = low('db.json');
app.db = db;
In your controller (accounts.js), access your database with req.app.db :
exports.login = function(req, res){
var email = req.body.email;
var password = req.body.password;
var db = req.app.db;
}
It looks like you can probably just setup and use the db directly in your routes file since you're not actually using the db in your main file. If it's that simple then I'd opt for doing that:
// routes file
var low = require('lowdb');
var db = low('db.json');
Alternatively, if you need to setup your db in the main file and pass it into the routes file and/or other modules, instead of exporting the router directly in the routes file, export a function which takes the db as input and returns the router. Then you can pass the db into the routes module when you require it.
// routes file
module.exports = function (db) {
// router setup using the input `db`
// ...
return router;
}
// index.js
var low = require('lowdb');
var db = low('db.json');
var routes = require('./routes')(db);
app.js
var express = require("express");
var app = express();
var path = require('path');
var db = require('./db');
var bodyParser = require('body-parser');
app.listen(80);
app.set('view engine', 'jade');
app.set('views', "./views");
// app.get('/', _GetMainPage);
// app.get('/sites', _GetSites);
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({ extended: true })); // Support encoded bodies
app.use(bodyParser.json()); // Support json encoded bodies
app.use(require('./controllers'));
./controllers/index.js
var express = require('express');
var router = express.Router();
router.use('/', require('./sites'));
router.use('/site', require('./site'));
module.exports = router;
./controllers/sites.js
var express = require('express');
var router = express.Router();
var site = require('../models/site');
router.get('/', function(req, res) {
site.getAll(function(err, rows){
if(err) {
res.send(err);
return;
}
res.render('sites', { sites : rows });
});
});
./controllers/site.js
var express = require('express');
var router = express.Router();
var site = require('../models/site');
router.get('/site', function(req, res) {
// console.log("get /site received. req.body: " + req.body);
res.render('site', {
site: {
name : req.params.name
}
});
});
module.exports = router;
When I request localhost/site I get a response saying:
Cannot GET /site
localhost/ works perfectly
I have been looking at this for a while and can't find the problem yet. If there is anything I can add, let me know. Thanks.
Thank you to the person that commented with the answer:
What happens if you navigate to /site/site? Your site.js route is relative to the route you provided in use. So it should be router.get('/' ... not router.get('/site' ...
The ./controllers/site route is already being routed to /site. On top of this I was calling router.get('/site', ...). This means it was actually routing to /site/site.
The solution is to just use router.get('/', ...) in the site.js file instead.
This really helped me, thank you.
Basically, the root path in the sub-app is defined in your core app where you mount it via the app.use() method.
the best example I can find from app.mountpath docs is here:
https://expressjs.com/en/4x/api.html#express.router
The app.mountpath property contains one or more path patterns on which a sub-app was mounted.
var express = require('express');
var app = express(); // the main app
var admin = express(); // the sub app
admin.get('/', function (req, res) {
console.log(admin.mountpath); // /admin
res.send('Admin Homepage');
});
app.use('/admin', admin); // mount the sub app
It is similar to the baseUrl property of the req object, except
req.baseUrl returns the matched URL path, instead of the matched
patterns.
If a sub-app is mounted on multiple path patterns, app.mountpath
returns the list of patterns it is mounted on, as shown in the
following example.
var admin = express();
admin.get('/', function (req, res) {
console.log(admin.mountpath); // [ '/adm*n', '/manager' ]
res.send('Admin Homepage');
});
var secret = express();
secret.get('/', function (req, res) {
console.log(secret.mountpath); // /secr*t
res.send('Admin Secret');
});
admin.use('/secr*t', secret); // load the 'secret' router on '/secr*t', on the 'admin' sub app
app.use(['/adm*n', '/manager'], admin); // load the 'admin' router on '/adm*n' and '/manager', on the parent app
I've created a node application with express. I try to separate the following layers which will give me the ability to test the application with unit testing...
The problem is that I don't know how to call to the router.js file which will stops in the post/get/delete application.
The server.js file looks as follows
http = require('http'),
app = require('./app')(),
http.createServer(app).listen(app.get('port'), function (err) {
console.log('Express server listening on port ' + app.get('port'));
});
This is the app.js file
var express = require('express'),
logger = require('morgan'),
bodyParser = require('body-parser'),
routesApp = require('./ro/route');
module.exports = function () {
var app = express();
app.set('port', process.env.PORT || 3005);
app.use(logger('dev'));
app.use(function (req, res, next) {
res.set('APP', 'User app');
next();
});
app.use(bodyParser.json());
app.use(routesApp);
return app;
};
This is the router.js, which will route the call to other module according to the http type like post/delete/get etc...
var handleGet = require('../controller/handleGet');
var handlePost = require('../controller/handlePost');
var express = require('express');
module.exports = function (app) {
var appRoute = express.Router();
app.use(appRoute);
appRoute.route('*')
.post(function (req, res) {
handlePost(req, res);
})
.get(function (req, res) {
handleGet(req, res)
})
Currently I've two questions:
How to make it work since when in debug It dump in
app.use(appRoute); on the router.js file?
The error is TypeError: undefined is not a function
Is it good way to structure the node app like in my post? I want to seperate all this layers like SOC, I'm fairly new to node and express and I try to build it to be modular and testable...
How to make it work since when in debug It dump in app.use(appRoute); on the router.js file? The error is TypeError: undefined is not a function
This fails because you don't pass app into the module when you require it in app.js, you would need to do something like
app.use(routesApp(app)); // <- this hurts my eyes :(
Is it good way to structure the node app like in my post?I want to sperate all this leyrs like SOC,I fairly new to node and express and I try to build it to be modular and testable...
Your definitely on the right track, keeping things separated is generally always a good idea. Testing is definitely one of the big pluses but it also helps with other things like maintainability & debugging.
Personally, I would make use of the bin directory for any start up script configuration
bin/www
var app = require('./app');
app.set('port', process.env.PORT || 3005);
var server = app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
This will help decouple your express app from all the environment setup. This should keep your app.js clean and only contain app-related config
app.js
var express = require('express')
, app = express()
, logger = require('morgan')
, bodyParser = require('body-parser')
, routes = require('./routes.js');
app.use(logger('dev'));
app.use(function (req, res, next) {
res.set('APP', 'User app');
next();
});
app.use(bodyParser.json());
app.use('/', routes);
...
module.exports = app;
Then finally, your routes.js should do nothing but handle your URLs
routes.js
var express = require('express')
, router = express.Router()
, handleGet = require('../controller/handleGet')
, handlePost = require('../controller/handlePost');
router.get('/', handleGet);
router.post('/', handlePost);
...
module.exports = router;
I have loads of router.get functions in my code which I think, could be reduced to a single switch-case function. Here is what I have tried:
function handlerA(req, res) {}
function handlerB(req, res) {}
var routes = {
'/url-one': handlerA,
'/url-two': handlerB
}
router.get('/*', function(req, res) {
var url = req.url;
if (routes[url]) {
routes[url](req, res);
}
});
This works but also, significantly slows my application. Is there any other solution which would not hit the performance of my app?
Thanks
Is there a reason you don't want to use router.get functions? I would guess express.js is internally performing the same logic that you are doing anyway. You are just replacing get functions with handlers.
If you are using similar logic between multiple routes, that may be worth abstracting.
I usually go with a setup like this:
app.js
routes.js
api/
user/
index.js
user.controller.js
user.model.js
image/
index.js
image.controller.js
image.model.js
/api/user/index.js:
var express = require('express');
var controller = require('./user.controller');
var router = express.Router();
router.get('/', controller.index);
router.post('/', controller.create);
module.exports = router;
/api/user/user.controller.js:
var User = require('./user.model');
exports.index = function(req, res) {
// Show list of users
};
exports.create = function (req, res, next) {
// Create user
};
/routes.js:
module.exports = function(app) {
// Insert routes below
app.use('/api/users', require('./api/user'));
app.use('/api/images', require('./api/image'));
// All undefined asset or api routes should return a 404
app.route('/:url(api|auth|components|app|bower_components|assets)/*')
.get(errors[404]);
// All other routes should redirect to the index.html
app.route('/*')
.get(function(req, res) {
res.sendfile(app.get('appPath') + '/index.html');
});
};
And lastly, the /app.js:
// Set default node environment to development
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var express = require('express');
var mongoose = require('mongoose');
var config = require('./config/environment');
// Connect to database
mongoose.connect(config.mongo.uri, config.mongo.options);
// Populate DB with sample data
if(config.seedDB) { require('./config/seed'); }
// Setup server
var app = express();
var server = require('http').createServer(app);
require('./config/express')(app);
require('./routes')(app);
// Start server
server.listen(config.port, config.ip, function () {
console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
});
// Expose app
exports = module.exports = app;
Most of this is directly from the Yeoman Generator Angular-Fullstack and it has a really nice setup!