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!
Related
I am new to node.js. I have the below program called app.js running on my laptop
var express = require('express');
var engine = require('consolidate');
var app = express();
app.set('views', __dirname);
app.engine('html', engine.mustache);
app.set('view engine', 'html');
app.use(express.static('.'));
app.get('/', function(req, res) {
console.log("Got request for index.html");
res.render('index.html');
});
app.listen(5000, '0.0.0.0');
and I have index.html in the same path as app.js. When I tried to hit localhost:5000 from browser or from postman, I am getting index.html as response. I thought app.get() will be called and the log "Got request for index.html" will be displayed. But that function is not getting called. I understood that .get() is called for routing.
I am not sure how browser / postman is getting index.html as response if get() in app.js is not getting hit. Can any one please help me to understand how postman is getting response even though app.get() is not getting called.
Express middlewares are applied in order. express.static doesn't call the next middleware in the chain if it matches on a file - it just serves the file and ends the response. So because index.html is in your static files directory it means the app.get middleware is never called.
Generally you'd create a public folder for your static assets and a views folder for your HTML views.
something like:
const {join} = require('path');
app.set('views', join(__dirname, '/views'));
app.engine('html', engine.mustache);
app.set('view engine', 'mustache');
app.use(express.static(join(__dirname,'/public')));
and make sure your index.html is in the views folder and your static assets are in the public folder
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.
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!
In my node app, I configured the views folder, later simply I am passing the html name alone. now the html file need to load from using the views config + html file right. ( am I wrong!)
But it's not working. any one give me the suggestion please?
here is my code :
var express = require('express'),
http = require('http'),
jade = require('jade'),
app = express();
app.set('view engine', 'jade');
app.set('views', __dirname + '/views'); // i configured the path so i am passing file name alone on get.
app.get('/', function(req,res){
res.sendfile('index.html'); //it's not working
res.sendfile('views/index.html') //it works
});
http.createServer(app).listen(3000, function () {
console.log('Express server listening on port ');
});
thanks in advance
You appear to have a misconception about what the view engine is. The view engine takes some non-HTML code, and transforms it into HTML. Here, you have it set to use jade.
The view engine is only good with the res.render() function. res.sendfile() merely sends a file from the current directory -- not the views directory.
Using express if you want to serve some static HTML files. You can just put those files directly in public folder.
When server will get a GET request of / it will search for /public/index.html serve that as response. You don't have to add router for that /.
Other wise if you want to use some template views then you have to use some views engine.
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.