node does not serve images to remote client - node.js

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!

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.js express multitenant app

We are planning a multi tenant app in node.js and express with mongodb. We would have some core functionality exposed via REST APIs. We would have customized functionality for each customer which would process some data and in turn call the REST APIs. The customized functionality as well as the UI(views and public folder) for each customer would reside in a separate folder for each customer under a parent folder. My questions are:
When I add a new customer and create a module for him, I should be able to start using it without having to restart the main module (app.js).
I need to redirect the customer to his UI files based on the urls say example.com/cust1/home would go to the parentfolder/cust1/views and example.com/cust2/home would go to parentfolder/cust2/views.
For the prototypes we have node running behind an apache proxy/reverse proxy and run the custom apps on their own ports. But in production we cant have the apps running on so many individual ports or modify the default.conf each time. For the UI we have tried the dynamic routing of express but that didnt work. We tried doing this in app.js:
var cust1 = require('./cust1/custommodule'); //this needs to be done for each new customer module added;
var app = express();
// view engine setup
var basePath = ''; //works if we hard code it to cust1 or cust2 here;
app.use('/', function(req, res, next){
// basePath = path.dirname(req.originalUrl); //this does not work since the app.set is executed before this is assigned
app.engine('html', require('ejs').__express);
app.set('view engine', 'html');
app.set('views', path.join(__dirname, basePath + '/views'));
next();
});
app.use('/:custom', express.static(__dirname + basePath + '/public'));
app.use('/cust1', cust1); //request coming with /cust1 should use this module, but this again needs to be done for each new customer module added;
Not sure if this is the right way. Any pointers would be appreciated.

Using holder.js on node.js with express web framework does work

I am using node.js with express web framework and with jade template parser.
I am following the bootstrap examples and have problems with holder.js.
I have put holder.js script into static files (public directory), having in app.js the following:
app.use(express.static(path.join(__dirname, 'public')));
I wanted to display some images with following code in jade template:
img(data-src='/holder.js/500x500/auto')
And it does not work. I checked through the browser and I am able to see holder.js file correctly, but adding any parameters is causing that main page is displayed instead.
I am suspecting that static files handler thinks that there is no such file as holder/500x500/auto and redirects to main page. How can I fix that?
Here is a Runnable with Express and Jade with a couple of Holder placeholders: http://runnable.com/U6IlNqsTu-cR6ZT8/holder-js-in-express-using-jade-for-node-js-and-hello-world
The two placeholders use different themes, with one using the auto flag.
server.js:
var express = require('express')
var app = express();
app.set('view engine', 'jade')
app.get('/', function(req, res){
res.render('index')
})
var server = app.listen(80, function(){})
views/index.jade:
doctype html
html
body
script(src='//cdnjs.cloudflare.com/ajax/libs/holder/2.3.1/holder.min.js')
img(data-src='holder.js/200x200/lava/auto')
img(data-src='holder.js/200x200/sky')
Take the leading slash out of the data-src attribute: holder.js/500x500/auto.

Express.js vhost subdomain set up

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.

Resources