I just set up a NodeJS server and wanted to use the vhost function from Express to allow for easy project setup. I want to be able to create a new directory for a new project without going through the hassle of creating new subdomains etc.
Basically, I want to be able to reach project1 at node.domain.com/project1 and project2 at node.domain.com/project2.
I'm now running server.js, which is located in the root (node.domain.com/server.js) and contains:
var express = require('express');
var app = express();
app
.use(express.vhost('node.domain.com', require('./project1/app.js').app))
.use(express.vhost('node.domain.com', require('./project2/app.js').app))
.listen(3000);
It's all working fine, when I go to node.domain.com/project1 I get the results of ./project1/app.js but whenever I change something in any of the app.js files it requires a restart (Ctrl+C followed by node server.js) for it to update the changes.
And the contents of app.js are, for example:
var express = require('express');
var app = exports.app = express();
app.get('/project1', function(req, res){
res.send('Hello World [/project1]');
});
Any idea why this is?
A thing to note is that I also run Apache on the same server, and I'm using this guide to allow both servers to run on port 80 (but accesible via different subdomains).
Oh! I feel dumb now, seems like I need to use something like forever, with its w flag.
Related
I need to run an angularjs application using a virtual prefix on an express server.
index.js
app.use('/foo', express.static('./index.html'))
This works in serving my index.html with the url 'http://localhost:3000/foo', but I need to use 'http://localhost:3000/foo/bar/'
I had thought this line of code would fix that issue, but it just breaks everything.
app.use('/foo', express.static('./index.html'))
Does anyone know how I can do this?
UPDATE
I have managed 'http://localhost:3000/foo/bar' by adding this
const server = express();
const app = express();
server.use('/foo/bar/', app);
But this results in a long list of 404 errors for all my js files. For example:
http://localhost:3000/app/homeCtrl.js
I'm currently developing using ng serve, with proxy configuration, while the proxy points to another nodejs instance on the same machine.
This backend is a simple express server, like this (simplified):
var express = require('express');
var app = express();
var customers = require('./customers.controller.js');
app.get('/api/customers', customers.getAll)
var server = app.listen(8081)
The frontend (ng serve) runs on port 4200 and proxies /api to http://localhost:8081/api
As far as I can see, this is the recommended setup.
However, I would prefer to have the backend running directly inside of ng serve instance instead of the proxy.
And if possible, even take advantage of the automatic reload feature of ng so that I don't have to restart the server if I change something on the backend code.
As both are nodejs and ng seems to be configurable, I think this is possible, but I can't find a starting point for defining my own routes
Its possible to do this
you just need to put your angular into the backend by utilize the nodejs routing
basicly angular is a "static file" and the entry point is coming from the index.html
// Redirect all the other resquests
this.app.get('*', (req, res) => {
res.sendFile(path.resolve('dist/index.html'));
});
but remember you need to handle the routing for image, js, css and others also.
So this is more of an open ended question: I've started working with node and I've been creating command line applications for practice. The majority of these apps take command line arguments and make http requests to an API and serve up the results based on the arguments passed. The thing is, I would like these programs to have useful front-end interfaces so that the results are not just display via the command line terminal. Is there an easy way to accomplish this? Is this what Express is useful for?
perhaps more fully, that's what express is for and that's what routes do for you - so that your browser can be directed to a default (e.g. index.html) page or a specific page or service. If you're rendering basic html pages, stored in an /HTML folder, to the user, then you might have the following kind of code in your app:
var express = require('express');
var app = express();
app.engine('html', require('ejs').renderFile);
app.use(express.static(__dirname + '/HTML'));
followed by a series of app.get('path/from/browser') and/or app.post('path/from/broswer') statements which tell your nodejs server what to do when various get and post commands are sent to the app.
as your app gets more complex, you may want to consider the router service as a way to structure your application code and associated services.
you also need to start an http server, so the browser can actually talk to the server. You would do that in a very simple way by executing the following code:
var cfenv = require('cfenv');
var appEnv = cfenv.getAppEnv();
app.set('port', appEnv.port);
var server = app.listen(app.get('port'), function() {console.log('Listening on port %d', server.address().port);});
In this simple example, your app is now using 3 new services: express, ejs, and cfenv. You would use the standard npm install process to get this into your local app so that you can use them. From your application root folder, you would execute npm install --save express, repeating for each of the three new services.
I have a an existing website built with express and I would like to add a "/blog" powered by Ghost. I've added Ghost to my dependencies, installed and configured the urls in Ghosts config to localhost:3000/blog, but now I'm having trouble.
In my app.js I've added the following lines:
var blog = require('./routes/blog');
app.use('/blog', blog);
My blog.js looks like this:
var express = require('express');
var router = express.Router();
var ghost = require('ghost');
ghost().then(function (ghostServer) {
ghostServer.start();
});
router.get('/', ghost);
module.exports = router;
I'm pretty sure blog.js is incorrect.
Node is very limited to do is, for cases when ghost is not configurable as express middleware, which I believe is the case here.
That leaves you with Loadbalancers and DNS as solutions to this problem. On something like HAPRoxy or Nginx you could make those recirects on the /blog route, would need to cater for scripts that the HTML requires to load and to redirect them too.
This might be also better practice since you seperate the concerns.
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