I am a fairly experienced MEAN stack developer and I'm doing a nodeJS rewrite of an API our company uses that is written in Perl using NGINX. I am serving the login page through the simple node route:
app.get('/', function (req, res) {
res.render('/var/www/html/login.html');
});
All stylesheets are rendered correctly when this page is served through Perl. When I serve this document through node, however, all of my 'hrefs' throw 404: Not Found. My application uses the EJS engine.
The absolute paths of the files I'm trying to reference:
Login: /var/www/html/login.html
Bootstrap Darkly: /var/www/html/bootstrap/css/bootstrap.darkly.css
Jquery: /var/www/html/js/jquery.js
I have tested the following four href locations in login.html:
<link href="bootstrap/css/bootstrap.darkly.css" rel="stylesheet" />
<link href="/bootstrap/css/bootstrap.darkly.css" rel="stylesheet" />
<link href="../bootstrap/css/bootstrap.darkly.css" rel="stylesheet" />
<link href="../../bootstrap/css/bootstrap.darkly.css" rel="stylesheet" />
I can provide any additional information the community thinks would be useful.
Anyone know what I'm doing wrong? This is running on a VM in Ubuntu 15.04 server edition.
Additionally, this may be somehow related to this question.
Here is my full app.js:
var express = require('express');
var http = require('http');
var path = require('path');
var fs = require('fs');
var app = express();
var expressJwt = require('express-jwt');
var jwt = require('jsonwebtoken');
var json = require('json');
var bodyParser = require('body-parser');
var morgan = require('morgan');
process.env.PATH = '/var/www/html/';
// all environments
app.set('port', process.env.PORT || 3000);
//app.set('view engine', 'html');
//app.set('views', path.join(__dirname, '../../'));
app.engine('html', require('ejs').renderFile);
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', "*");
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// development only
if ('development' == app.get('env')) {
//app.use(express.errorHandler());
}
require('./routes/main.js')(app);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
A comment suggested I add:
app.use(express.static('/var/www/html'));
When this line is added to the top of the app.js file, just after the declarations, the browser downloads a perl script upon http://localhost:3000. In my eyes this is evidence that NGINX is a proxy to my node server under these circumstances.