I want to create a site with this structure:
--mysite.com
--mainserver.js
-----mysite.com/project1
-----server.js
-----mysite.com/project2
-----server.js
In each project folder I want to run a separate node application. Im trying to do this using vhost module.
In my mainserver.js I have this to test:
var express = require("express");
var app = express();
var router = express.Router();
var vhost = require('vhost');
var app2 = express();
app2.get('/', function(req, res) {
res.send("echo");
});
app.use(vhost('localhost/project1', app2));
app.get('/', function(req, res) {
res.send("hi");
});
var port = Number(process.env.PORT || 5000);
app.listen(port, function() {
//console.log("Listening on " + port);
});
When navigating to localhost:8000 I see the "hi". But when I navigate to localhost:8000/project1 I get Cannot GET /test...
Please help!
I believe this is what you are trying to achieve. (Without use of vhost)
project1/index.js
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
return res.send('project 1');
});
// Other routes specific to project 1 here
module.exports = router;
Project2 could be set up similarly.
server.js
var express = require('express');
var app = express();
app.use('/project1', require('./project1'));
app.use('/project2', require('./project2'));
var port = Number(process.env.PORT || 5000);
app.listen(port);
Related
I am trying to take a tutorial on node.js and express. I'm trying to use the debug feature, but it is not logging anything to the console.
console.log works fine...but using debug() does not
I am on windows and am trying to run the app with...
set DEBUG=app ; node app.js
var express = require('express');
var chalk = require('chalk');
var debug = require('debug')('app');
var app = express();
app.get('/', function(req, res){
res.send('Hello from my library app');
})
app.listen(3000, function(){
debug(`listening on port ${chalk.blue('3000')}`);
});
Try this:
var express = require('express');
var chalk = require('chalk');
var app = express();
var debug = require('debug')(app);
app.get('/', function(req, res){
res.send('Hello from my library app');
})
app.listen(3000, function(){
debug(`listening on port ${chalk.blue('3000')}`);
});
For example :
server.js file
var express = require('express'),
app = express(),
port = 3000,
routes = require('./app/routes/apiRoutes');
routes(app);
app.listen(port);
routes.js file
'use strict';
module.exports = function( app ) {
var api= require('../controllers/apiController');
app.route('/get').get(api.get);
};
apiController.js file
'use strict';
exports.get = function(req, res) {
// console.log( req.app ); // access it but it didn't work ?
// here want to access app to set cookie and changed cookie ?
};
if there is another way please help me thanks :)
If I correct understand your question, with routes you can do something like this:
In routes.js file:
var router = require('express').Router()
router.get('/home', function(req, res, next) {
res.render('index')
})
module.exports = router
In server.js file:
var mainRoutes = require('./routes.js')
app.use(mainRoutes)
Best way (in my opinion) to use controllers from another file it's use exports.functionName notation:
In someController.js file:
exports.homePage = function(req, res) {
res.render('index')
}
So, your router will looks like this:
var router = require('express').Router()
var someController = require('./someController.js')
router.get('/home', someController.homePage)
module.exports = router
Do different way, use route in app.use
app.js:
const
express = require('express'),
app = express(),
port = 3000,
routes = require('./app/routes/apiRoutes');
app.use(routes);
app.listen(port);
apiRoutes.js:
const
router = require('express').Router(),
apiController = require('../controllers/apiController');
router.get(
'/get',
apiController.get);
module.exports = router;
Check this example: app.js , some route file
My first piece of code below:
var express = require('express');
var app = express.createServer(express.logger());
app.get('/', function(request, response){
response.send('Hello World 2');
});
var port = process.env.PORT || 5000;
app.listen(port, function(){
console.log("Listening on " + port);
});
Threw up the error: "Most middleware (like logger) is no longer bundled with Express and must be installed separately"
So I looked at StackOverflow, did npm install morgan, and changed my code to:
var express = require('express');
var logger = require('morgan');
var app = express.createServer(logger());
app.get('/', function(request, response){
response.send('Hello World 2');
});
var port = process.env.PORT || 5000;
app.listen(port, function(){
console.log("Listening on " + port);
});
Now I get this error:
var app = express.createServer(logger());
^
TypeError: Object function createApplication() {
var app = function(req, res, next) {
app.handle(req, res, next);
};
mixin(app, proto);
mixin(app, EventEmitter.prototype);
app.request = { __proto__: req, app: app };
app.response = { __proto__: res, app: app };
app.init();
return app;
} has no method 'createServer'
That is because createServer method has been removed from express.
use
app = express();
app.use(logger())
instead of
app = express.createServer(logger())
Many things got changed from express 3.0 to 4.0. You should have a look here
You should create app with express(). Afterwards, you can setup any middleware (like morgan in this case) with app.use:
var express = require('express');
var logger = require('morgan');
var app = express();
app.use(logger());
...
app.use() documentation: http://expressjs.com/4x/api.html#app.use. (Linking to Express 4.x documentation as not explicit what Express version you're running).
There is an exact same example like the one I wrote above in Morgan's README in GitHub.
I'm using express framework to run many node applications basically as different node instances. Is it possible to run all the applications as single node instance (like routing with different url and same port) ?
Sure:
var express = require('express');
var main = express();
var app1 = express();
var app2 = express();
main.use(app1);
main.use(app2);
app1.get('/app1/test', function(req, res) {
res.send('handled by app1');
});
app2.get('/app2/test', function(req, res) {
res.send('handled by app2');
});
main.listen(3012);
If each app has their own unique URL prefix, you can also use this:
var express = require('express');
var main = express();
var app1 = express();
var app2 = express();
main.use('/app1', app1);
main.use('/app2', app2);
app1.get('/test', function(req, res) { // GET /app1/test
res.send('handled by app1');
});
app2.get('/test', function(req, res) { // GET /app2/test
res.send('handled by app2');
});
main.listen(3012);
Why doesn't the following work ?
app.js
reports/
├── index.js
└── batch.js
in app.js :
app.use('/reports', require('./reports')
in index.js :
var express = require('express');
var batch = require('./batch');
var app = express.createServer();
...
app.use('/batch', batch);
module.exports = app;
in batch.js :
var express = require('express');
module.exports = function() {
var app = express.createServer();
console.log('I am here');
app.get('/', function(req, res) {
console.log('I am there');
});
return app;
};
calling GET /reports/batch prints I am here but doesn't print I am there
Can anyone pinpoint me to the problem ?
Thanks
try this:
in app.js :
var express = require('express'),
http = require('http'),
path = require('path');
var app = express.createServer();
require('./reports')(app);
in reports/index.js :
module.exports = function(app){
var batch = require('./batch')(app);
app.use('/batch', batch);
}
in batch.js :
module.exports = function(app) {
console.log('I am here');
app.get('/', function(req, res) {
console.log('I am there');
});
};
Note that you may need to modify the app.get routing as needed. but basically the idea here is instead of calling createServer all the time just keep passing it down the chain from one module to the next.
Hope this helps!
Here is my app.js. It passes db and app to the feature.js. Basically they share the same app variable.
in app.js
var express = require('express'),
http = require('http'),
path = require('path'),
mongoose = require('mongoose'),
db = mongoose.connect('mongodb://abc:123#xxxx.mongohq.com:90000/app123423523');
var app = express();
//here you set app properties
require('./routes/feature').with(app, db);
in feature.js
module.exports.with = function(app, db) {
//do work
}
It seems that I have forgotten javascript a little.
I was doing
app.use('/batch', batch);
batch is equal to
function() {
var app = express.createServer();
console.log('I am here');
app.get('/', function(req, res) {
console.log('I am there');
});
return app;
};
Instead I should have done
app.use('/batch', batch());
Which equals to what express.createServer() returns which is what app.use expects to get