I have a situation where I have tow express js apps App1 and App2 running on port 80 and 8080 respectively. I have declared these apps in same js file.....now I wanted to share some data from App2 request to App1. Is it possible if yes how I can do that? or can we forward App2 request to App1 directly.
You can easily do this with vhost() in Express. All you have to do is
var express = require('express');
var app = express();
app
.use(express.vhost('localhost', require('/path/to/website1/app.js').app))
.use(express.vhost('localhost2', require('/path/to/website2/app.js').app))
.listen(3000);
This will listen on port 3000, and check for specific host names - in this case, localhost and localhost2. If one of those hosts match, it will get an instance of their 'app' to run/use.
In this case you will have to export your app object from app.js to use all the methods and API's you have, You can simply do this by
var express = require('express');
var app = exports.app = express();
Related
My needs are simple, I want the ReactJs frontend served from an ExpressJs server, without crazy separate ports and stuff.
Is it possible to send HTTP requests to my server for APIs as well?
How can I do this?
Yes and yes.
You create the react site. Use the dev server to develope it and make it look nice. When its ready you build it by doing
npm run build
Then you create the express server and make it serve the static html files
const express = require('express')
const app = express()
app.use('/', express.static('dist'))
app.listen(3000, () => { console.log('running on 3000') })
Now you can open the express app on server 3000 and it serves the react site.
I have a React front-end app with an express backend that's working locally as a proxy API server.
In my local dev environment, the React fonr-end is running on port 3001, with the Express server running on port 3000.
In my package.json, I have the proxy set up:
"proxy": "http://localhost:3000",...
The app is deployed via an Azure pipeline, which builds the app and the server, zips them up, and copies the artefacts to an Azure domain.
In the Azure app's Configuration, I have the following Startup Command:
node server & npx serve -s
This should (in theory) start the Express server (which is set to listen on port 3000) and server the front-end. The front-end is indeed accessible, but when it tries to make a call to the api via a simple fetch request, it's returning the HTML of the React app instead of a JSON response. This indicates to me that it's not actually exposing the Express port to the front end.
Is it actually possible to server the Express server and the frontend as part of the same container?
It turns out that this approach - while it was the wrong one - was in fact working, but Azure App Services will only expose a single port. In this case, we could either expose the React front end via npx serve -s OR expose the api by setting the PORT variable to 3000. Both were running, but only one could be reachable.
The solution was to serve the React front-end via the Express server:
const path = require('path');
const { response } = require('express');
const express = require('express');
const request = require('request');
const app = express();
app.use(express.json());
app.use(express.static(path.join(__dirname, '../')));
app.use('/api', function (req, res, next) {
// API function calls here
...
});
app.get('*', function(req, res, next) {
res.sendFile(path.join(__dirname, '../index.html'));
});
app.set('port', 3000);
app.listen(app.get('port'), () => {
console.log('Proxy server listening on port ' + app.get('port'));
});
And set the PORT container variable to 3000.
I was referring to some online tutorials for establishing a Node server using Express 4. I will make my question very simple and easy to understand.
The main app.js file has the following lines (other code lines like middlewares etc. are not show here)
var express = require('express');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
app.use('/', routes);
app.use('/users', users);
I have tested the above code. Included the index.js and users.js inside the routes folder. This worked perfect. This means that the http server is already created.
But, my confusion raised, when I say another type of coding done in another site. It has the following lines of code.
var express = require('express'),
routes = require('./routes'),
http =require('http’);
var app = express();
My first confusion is, why do we need to use the http middleware.
The code further creates a server like this
var server = http.createServer(app);
Since, I am using the Express framework, why do we need to create the server, this way
Reference can be found here https://github.com/azat-co/practicalnode/blob/master/ch5/blog-express/app.js#L72
Any help would be highly appreciated. Thanks in advance.
Perhaps the developer wanted to create a raw http server for some other specific use later on? Strictly speaking, it is not necessary to do that.
The following is perfectly sufficient to create an http server and begin listening for connections using express:
var express = require('express');
app = express();
app.listen(3000);
in express best way is:
app = express();
app.listen(3000);
in theory this:
var server = http.createServer(app);
could be used to reuse http server, for example to run sockets.
But app.listen also return http server like http.createServer(app);
We can do:
var server = http.createServer(app);
var io = require('socket.io')(server);
But we also can:
var server = app.listen(3033);
var io = require('socket.io')(server);
When createServer(app) may be useful? if we want listen to http i https:
http.createServer(app).listen(80);
https.createServer(options, app).listen(443);
I have two Express JS application and I want to add a route for one inside the other so when this link is requested it goes to the sub-application. Is that possible??
Example:
The main application is accessed through the link www.linktoapp.com and it is written in Express js.
Now I have developed another Express js application and I want to access it through the link www.linktoapp.com/secondapp
My question is can I add this route (/sceondapp) in the main application so I can access it like I said?
Thanks.
You could always have two separate express processes running on 2 ports and then reverse proxy the requests.
https://github.com/nodejitsu/node-http-proxy
Your question is a bit confusing! What I understand from your question:
You have two expressjs application running. Two application can't run on same port. Two application can however run on two different ports. See example below.
var express = require("express");
var app1 = express(); //created the first app
var app2 = express(); //created the second app
app1.get("/",function(req,res){
res.send("<html><body><a href='/sec'>go second app</a></body></html>");
// created a link to app2
});
app1.get("/sec",function(req,res){ // redirection to second app
res.redirect("http://localhost:3001/");
})
app2.get("/",function(req,res){
res.send("welcome to second app");
});
app2.listen(3001,function(){ // app2 is listening on port 3001
console.log("app two is listening on 30001 ")
});
app1.listen(3000,function(){ // app2 is listening on 3000
console.log("app1 is listening on 3000");
});
What you want is to create an Express sub-app. Express sub-apps share routes, views, sessions, etc with the main app.
For example, let's say you want one node app, s_app.js, to be a sub-app of another, p_app.js.
s_app.js
var express = require('express');
var app = module.express = express();
// set routes for userjs
app.get('/path_1', function(req, res){
// display path_1
});
p_app.js
var express = require('express');
var app = express();
var s_app = require('s_app'); // mount s_app.js
app.use(s_app);
// now p_app and s_app share routes, views, sessions, etc
If you want more detail, take a look at a tutorial I wrote on how to build node.js sub-apps
Trying to set up connect's vhost middleware. Would love some help.
I've got my normal express.js app, with node_modules, public, views and routes directories. I've added another directory next to those which holds another express.js app.
I've added this line to my top level app (tedxgramercy):
app.use(express.vhost('chatter.tedxgramercy.com', require('./chatter/app.js').app));
And this line to my chatter app:
var app = exports.app = express();
The chatter app calls listen on port 8000, the main (top level) app calls listen on port 3000. I don't know if that's right.
When I launch my app (node app) it runs fine and I can access both apps on localhost:3000 and localhost:8000 respectively, but when I deploy to my server, the subdomain http://chatter.tedxgramercy.com doesn't work.
Any pointers? Do I have to change my DNS to point to the other port or something?
It's a simple, but somewhat tricky setup.
First, the main app.js:
var vhost = require('vhost');
app.use(vhost('chatter.tedxgramercy.com', require('./chatter/app').app))
app.use(router);
I included the router to make it clear that it is critical for it to be used after configuring virtual hosts.
Then, in chatter/app.js:
var express = require('express');
var app = express();
var path = require('path');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
var router = express.Router();
router.get('/', function(req, res, next) {
res.render('index');
});
app.use(router);
exports.app = app;
This is the bare minimum setup to render a Jade template in a sub app. Notice that app is exported, but no server is actually started since the main app is the server.