Express.js vhost subdomain set up - node.js

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.

Related

NodeJS Express Website Error 403 on public server

I have a super simple NodeJS Express site setup with the following Folder Structure:
Views just contains a singe file index.ejs and public just a few .css and .js files required to make my site work (bootstrap, jquery etc.).
Using this on my local machine works great, however, the moment I put it on my live server (Shared Hosting on A2 Hosting), trying to open the page gives me Error 403, any ideas of what I'm missing?
Here is my servers.js file:
var express = require('express'),
path = require('path'),
nodeMailer = require('nodemailer'),
bodyParser = require('body-parser');
var app = express();
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.get('/', function (req, res) {
res.render('index');
});
Have you tried all steps in their tutorial?
https://www.a2hosting.com/kb/installable-applications/manual-installations/installing-node-js-on-managed-hosting-accounts
Found it on StackOverflow:
https://stackoverflow.com/a/32535632
PS: I would prefer to write this as a comment, but I don't have enough reputation yet.

Node.js: 404 File not found

I'm new to Node.js and trying to deploy my first app on a windows server 2016 using IISNODE with IIS. Everything working perfectly on my localhost, when I moved the site to the server all static files are getting 404 error.
I put the website directly into the wwwroot folder. Please let me know if additional information is needed.
When hoovering my cursor over the 404 file the url "http://mydomain[.]com/star_wars_movie_app/images/star_wars_episode_1_poster.png" which looks correct.
public folder consist of the following:
css
images
javascript
var express = require('express');
var app = express();
app.set('view engine', 'ejs');
var routes = require('./routes');
var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
// Home
app.get('/star_wars_movie_app', routes.home);
// movie single
app.get('/star_wars_movie_app/star_wars_episode/:episode_number?', routes.movie_single);
app.listen(process.env.PORT || 3000);
Any suggestions would be greatly appreciated, thanks in advance!

node does not serve images to remote client

I am new to Node. I tried a hello world Express/Node application in which I am displaying a small text and some static images and some styling in a static css file. I am running my application on a hosted virtual machine using Vagrant on a computer I own.
The issue I am facing is that when I try the application from another remote computer in the same LAN, some images are not displayed. The corresponding GET requests are logged in Node with 200 status code but my browser is waiting forever for the answer. I tried Firefox, Safari and Chrome. The weird thing is that if I call the same application from the same computer hosting the virtual machine but outside the virtual machine then I can see the images.
Also if when I deployed the same application on a heroku account I could see all the images from the computer which could not display them.
So I really don't understand what could be wrong.
Please tell me if you faced a similar issue before.
Here is the code
var express = require('express');
var logger = require('morgan');
var path = require('path');
var morgan = require('morgan');
var app = express();
var router = express.Router();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(router);
app.use(logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));
router.all('/', function (req, res, next) {
console.log('Someone made a request!');
next();
});
router.get('/', function (req, res) {
res.render('index');
});
app.listen(3000);
module.exports = app;
Here is the simple page layout. The two images are in the same folder.
doctype html
html(lang="en")
head
title Express Routing
body
h1 Express Routing
p The Definitive Guide
img(src='/images/test.ico')
img(src='/images/giraffe.jpg')
And here is the network trace when I call the application click here.
Eventually, I tried to use another sample application from an existing public git repository and had the same issue when I deployed it on my virtual machine (not all the static files are downloaded from a REMOTE machine whereas they all download if I call the application from the host machine).. As you can see 3 static files are pending download:
click here.
For your information when I use Rails instead of Node I don't have such issue.
the error was gone after I installed the latest ubuntu updates.
Thanks for your time!

Link one express js application to another

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

NodeJS, Express, and plugin stylesheets

I am using Express v3.0.0rc1 with Node v0.8.1 and I am trying to create a webserver that works off of a modular system for displaying routes and views.
Is there a way to display stylesheets and client-side javascript files from within the ./modules/foo/ directory instead of ./public when display requests from within the foo module routes?
My approach would be to set up an additional static middleware layer. This has the limitation that the static files used by your modules must have different names since any name conflicts will result in only the resource from ./public being served. According to this SO Setting up two different static directories in node.js Express framework, this may be the best you can do.
var express = require('express');
var http = require('http');
var app = express(); // Express 3+
var server = http.createServer(app);
app.configure(function(){
app.use(express.bodyParser()); // Parses incoming request date.
app.use(app.router); // Mount application routes
app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/module/foo'));
});
app.get('/', function(req, res) {
res.send("Hello World!");
});
server.listen(3000, 'localhost');
});

Resources