2

I'm from java background, i started using node js and im loving it now. I have read the other threads similar to my questions before im posting it in SO.

I have 3 different node application(app 1, app2, app3) Usually if its in java i will deploy three apps in tomcat and can access them locally like this localhost:8080/app1, localhost:8080/app2 etc. Im looking for similar approach in node js. I have read this thread and installed express globally and made a script called master.js with this code

var express = require('express'); var app = express();

app .use('/app1', require('./app1/server.js').app) .use('/app2', require('./app2/server.js').app) .listen(8080);

but im getting

TypeError: Cannot read property 'handle' of undefined

Since im new to node, im not sure is this process is complicated like setting proxy etc as mentioned in this thread

basically im looking for deploying all my apps in same port and access them like localhost:8080/app1, localhost:8080/app2

do i need nginx and proxy to achieve this ?

Also in ec2 instance i can run my node app by going into app1 folder and typing node server.js so the app will be listing in port 8080 but when i press ctrl c to do other task, it terminated the app.

Community
  • 1
  • 1
optimus
  • 729
  • 2
  • 12
  • 36
  • Depending on the app1, app2 server files, this logic should work, Do you export express instances from those files? you could show us what the server.js-s look like. – user1695032 Feb 10 '16 at 09:45
  • @user1695032: my app1 and app2 are separte express apps so server.js will have all my routes and other params i configured – optimus Feb 10 '16 at 09:54

2 Answers2

1

You need to use a variable for each app and then use it. Try:

var app1 = require('./app1/server.js').app
var app2 = require('./app2/server.js').app
app.use('/app1', app1).use('/app2', app2).listen(8080);
DiegoRBaquero
  • 1,208
  • 10
  • 14
  • 1
    Is there an automatic way of doing this ? using some external tool like pm2 or something like that, Is there any way to do it like in Apache (Just put another directory in it and you'r ready to go) ? – Pini Cheyni Jun 06 '17 at 11:26
1

You don't need nginx/proxy to handle this. From what i can see your code should work if your app1|app2/server.js-s are built like this:

var express = require('express');
var app = express();

app.get('/hello', function() {...})

module.exports = {
    app: app
}

now you can send GET requests to /app1/hello and /app2/hello

user1695032
  • 1,112
  • 6
  • 10
  • And I missed the Export part in server.js and instead configured to listen in server.js itself so exporting the app works now – optimus Feb 10 '16 at 10:22